C++ Program Parameter Problem
-
int main(int argc, char* argv[])
{
switch(argc)
{
case 1:
//.....
break;
case 2:
if(argv[1] == "-help")
{
cout << "Help" << endl;
}
//...
break;
default:
//... code...
break;
}return 0;
}
If the program in this case is named "prog.exe", when I type "prog.exe -help" into the command line, my understanding is that "Help" should be printed into the command line window. Any ideas as to why it isn't working? :confused: Also, if there is a better way to check to see which program parameters have been passed in than the way I am trying to do so, please let me know. I am open to suggestions. :)
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
-
int main(int argc, char* argv[])
{
switch(argc)
{
case 1:
//.....
break;
case 2:
if(argv[1] == "-help")
{
cout << "Help" << endl;
}
//...
break;
default:
//... code...
break;
}return 0;
}
If the program in this case is named "prog.exe", when I type "prog.exe -help" into the command line, my understanding is that "Help" should be printed into the command line window. Any ideas as to why it isn't working? :confused: Also, if there is a better way to check to see which program parameters have been passed in than the way I am trying to do so, please let me know. I am open to suggestions. :)
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
-
int main(int argc, char* argv[])
{
switch(argc)
{
case 1:
//.....
break;
case 2:
if(argv[1] == "-help")
{
cout << "Help" << endl;
}
//...
break;
default:
//... code...
break;
}return 0;
}
If the program in this case is named "prog.exe", when I type "prog.exe -help" into the command line, my understanding is that "Help" should be printed into the command line window. Any ideas as to why it isn't working? :confused: Also, if there is a better way to check to see which program parameters have been passed in than the way I am trying to do so, please let me know. I am open to suggestions. :)
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
bneacetp wrote: if(argv[1] == "-help") Herein lies your problem.
-
bneacetp wrote: if(argv[1] == "-help") Herein lies your problem.
Yep. I agree. I changed
if(argv[1] == "-help")
to
if(strcmp(argv[1], "-help") == 0)
and that solved the problem. Thanks for taking the time to point that out. :)
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
-
Yep. I agree. I changed
if(argv[1] == "-help")
to
if(strcmp(argv[1], "-help") == 0)
and that solved the problem. Thanks for taking the time to point that out. :)
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."
Unless you REALLY need to look exactly for "-help" I would recommend a case insensitive string comparison. Not all users rememebr you want all lower case for command line arguments, for example - see
stricmp
-
Unless you REALLY need to look exactly for "-help" I would recommend a case insensitive string comparison. Not all users rememebr you want all lower case for command line arguments, for example - see
stricmp
Alright. That certainly is a good suggestion. Thanks. :) I believe I will make that change. Thanks for taking the time to let me know about the
stricmp()
function. :)
John 3:16: "For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."