-
Trouble with argv[]
Hello,
How could I compare command-line arguments to strings? I've got the code below that works with single characters. What should I change to make it work with strings?
Code:
int main(const int argc, char* argv[]) {
bool v = false;
for(int i = 1; i < argc; i++) {
if(*argv[i] == 'v')
v = true;
}
Seron
-
use strcmp() for null terminated c style strings.
for stl strings use member function compare()
-
I'm a beginner at C++ and have not come across strcmp() or compare() . Are there any resources on the web to learn more about how these are used?
Seron
-
strcmp
strcmp, strcpy, strcat, and many others are part of the c library <string.h>
If you have any book on C++ or C it should talk about the use of these functions other wise this is how they work...
strcompare( string1, string2) will return 0 if they are equal < 0 if string1 is less than string 2 or > 0 if string2 is greater than string1.
e.g.
char string1[20] = "Mickey";
char string2[20] = "Pluto";
if( strcmp(string1, string2) == 0) result is false
if( strcmp(string1, string2) > 0) result is false
if( strcmp(string1, string2) < 0) result is true because Mickey < Pluto
In the case with the argv statements you would compare like this
if( strcmp(argv[1], "something") == 0)
..do something
else if( strcno( argv[2], "something") == 0)
...do something else
...
-
Thank you zMan.
One more question. Why is Mickey < Pluto ?
Seron