c++ arguments
Name:
Anonymous
2006-09-30 22:23
i compile
int main(int argc, char* argv[]) {
if(argv[1]=="f"){
printf("f");
}
}
but when i run "aids.exe f" it doesnt printf("f")
i demand an explanation
Name:
Anonymous
2006-09-30 22:45
you're not allowed to compare strings like that. either convert argv[1] into a std::string, or use strcmp().
Name:
Anonymous
2006-09-30 22:55
C strings use pointer equality: s == t iff s and t are pointers to the same object.
Name:
Anonymous
2006-09-30 23:44
strcmp() worked thx
Name:
Anonymous
2006-10-01 1:23
nevermind strcmp borked what i was working on. how do convert argv[1] to std::string?
Name:
Anonymous
2006-10-01 1:24
strcmp() returns 0 if equal.
Name:
Anonymous
2006-10-01 1:29
yes but if i try something like
if(strcmp(argv[1],stringtwo)==0){
balls();
}else if(strcmp(argv[1],stringthree)==0){
balls();
}else{usage();
}
it crashes when i give no arguments, but when i put an argument that matches stringthree it skips everything and does usage() anyway.
Name:
Anonymous
2006-10-01 1:30
ok, i think i can fix the crashing by checking if it even has an argument to compare. but how do i keep it from skipping the ifs?
Name:
Anonymous
2006-10-01 9:21
(sage)
Name:
Anonymous
2006-10-01 10:02
>>5
#include <string>
...
if (std::string(argv[1])=="f")
...
of course, if you're planning to do multiple comparisons with the same argument...
...
std::string s(argv[1]);
if (s == "f")
...
if (s == "g")
...
Bringing /prog/ back to its people
Newer Posts