c++ - Processing command line arguments, allowing for any order -
ok creating program allows user input in order. term -1 or -2 used indicate operating mode. while argument -c precedes filename. here loop have written allow these entered in order user wishes.
int main(int argc, char *argv[]) { string qcolors_name; string inputfilename; pmode_t mode = run_process2; // perform command line argument checking if(argc < 2){ cerr << "not enough arguments" << endl; return 0; } for(int = 1; < argc - 1; i++){ if(argv[i] == "-1"){ mode = run_process1; cout << "mode 1" << endl; }else if(argv[i] == "-2"){ mode = run_process2; }else if(argv[i] == "-c"){ qcolors_name = argv[i+1]; cout << qcolors_name << endl; i++; }else if(argc-1 == i){ break; }else{ cout << argv[i] <<endl; cerr << "arguments error"<<endl;; return 0; } }
however program seems dump out arguments error message though outputs argv[i] = -1. 1 of things have tested though , should never have reached case. im sure syntax wise doing wrong.
edit
here imput sample error output.
user> ./colorfun -1 -c qc_bw.txt mountains.ppm <- 9:46pm -1 arguments error
it's c, can't compare strings str1 == "smth". use strcmp()
instead.
more detailed explanations: operator ==
in c compares integral entities: numbers, pointers, etc. "smth"
pointer memory area in constant segment of program. that's why code like
const char* = "abc"; const char* b = "abc"; assert(a == b);
may give true, both & b may point single memory location. expression never compares letters "a", "b", "c" a
, b
, compares pointers. likewise in java have use str1.equals(str2)
because java operator == compares integral entities (numbers, characters , "pointers objects"). in opposite in c++, python , other programming languages operator overloading, ==
2 strings leads character character comparison.
Comments
Post a Comment