Someone asked me the other day when you are checking the console parameters for any passing in options how come something like
if (argv[i] == "-t") |
does not work, well it does make sense when you look at it, but because the “-t” is actually a char[] (a string) then this would occupy a place in memory and thus you are checking against memory locations which unless you are very very very lucky, this would never come out correct, you need to use the string compare (strcmp) function as below
#include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv) { for (int i =0 ; i < argc; i++) { // output argument that was passed in. cout << argv[i] << endl; // compare with "-t" option if (strcmp(argv[i],"-t")==0) cout << "the -t option" << endl; } return 0; } |
and the output would be something like
./consolet -t 40 ./consolet -t the -t option 40 |