• Commandline Arguments
    6 replies, posted
Hey i have a little problem just read this it will explain and it's just a short snippit: [cpp] //Why is this nawt working #include <iostream> using namespace std; int main(int argc, char *argv[]) { bool UseConsole = 0; if(!argv[1]) { cout << "No Command Line Arguments" << endl; } else { cout << "Arguments :D" << endl; for(int i = 1; i<argc; i++) { cout << argv[i] << endl; if(argv[i] == "-console") { UseConsole = 1; } } } cout << "Is argv[1] == to -console or console? ;3" << endl; if(argv[1] == "-console") { cout << "1" << endl; } else if(argv[1] == "console") { cout << "2" << endl; } else { cout << "3" << endl; } cin.get(); } //////////////////////////////////////// //////////////////////////////////////// Output //////////////////////////////////////// //////////////////////////////////////// Arguments ;D -console Is argv[1] == to -console or console? ;3 3 [/cpp]
Don't use 'using namespace std;' in the global scope, it's bad practice. Why are you assigning ints to bools, use the true / false keywords. [cpp] #include <iostream> int main( int argc, char *argv[] ) { for( int i = 0; i < argc; i++ ) { std::cout << "Argument " << i << " is \"" << argv[i] << "\"" << std::endl; } } [/cpp]
Also, use either std::string::compare() or std::strcmp to compare your strings; in your example you were only comparing the first character. I prefer the latter in this case, no need to construct std::strings just to do a comparison. [cpp]#include <iostream> #include <cstring> int main(int argc, char *argv[]) { bool UseConsole = false; if(argc == 1) { std::cout << "No Command Line Arguments" << std::endl; return 1; } for (int i = 1; i < argc; i++) { char* arg = argv[i]; if (!arg) break; std::cout << arg << std::endl; if (arg[0] == '-' || arg[0] == '/') { if (std::strcmp(arg+1, "console") == 0) { UseConsole = true; } else if (std::strcmp(arg+1, "myOtherArgument") == 0) { /* ... */ } } } if (UseConsole) std::cout << "use console was specified." << std::endl; std::cin.get(); } [/cpp]
[QUOTE=blankthemuffin;18060179]-snip-[/QUOTE] argv[0] is not among the arguments he wants to use. [QUOTE=voodooattack;18060293]in your example you were only comparing the first character.[/QUOTE] Actually, he is comparing the value of two pointers. Of course, that's not what he wants either. [QUOTE=voodooattack;18060293][cpp] if (!arg) break; [/cpp][/QUOTE] arg will not be null in that loop, so there is no reason to both check for null and check argc. If you wanted to terminate the loop on null, you should write it like this instead to put all the rules of the loop into the for-loop statement: [cpp] for(char** arg = argv[1]; *arg != NULL; arg++) { /*safely dereference arg in here*/ } [/cpp] If it was necessary to check both, it's best put like this: [cpp] for(int i = 1; i < argc && argv[i] != NULL; i++) { } [/cpp]
Oh thanks also why would i use true/false instead of 1/0
Because it's the language keywords for defining the value of a boolean. 1 and 0 need to be casted to the boolean type first, Visual Studio also displays a warning about this.
[QUOTE=jA_cOp;18061466]argv[0] is not among the arguments he wants to use.[/QUOTE] I know, he was wondering what he was receiving in argv, now he should know.
Sorry, you need to Log In to post a reply to this thread.