|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
What's wrong with this simple IF statement?
Sorry for the potentially stupid question, but I couldn't locate this basic info on the net.
A couple paremeters are passed to a program on command line. The second paremeter is in this case OH Here's the code... Code:
printf (argv[2]);
if (argv[2]=="OH") {
printf ("\n\ndetected OH. cool\n\n");
exit(0);
} else {
printf ("\n\ndidn't see OH. crap\n\n");
exit(0);
}
The program prints out OH to the screen, then says "didn't see OH. crap". How am I mucking up this if statement? |
|
#2
|
||||
|
||||
|
You can't compare strings with == in C. You have to use strcmp() for this.
Code:
#include <string.h>
printf (argv[2]);
if (strcmp(argv[2],"OH") == 0) {
printf ("\n\ndetected OH. cool\n\n");
exit(0);
} else {
printf ("\n\ndidn't see OH. crap\n\n");
exit(0);
}
Hope this helps! |
|
#3
|
||||
|
||||
|
Cool, strcmp() works good..
Is there a way to eliminate the if/else and just make this a if ( doesn't match arguemtns) { exit progrm } ? I tried changing the ==0 to !=0 but then neither of the arguments were recognized (even though they were correct). |
|
#4
|
||||
|
||||
|
Change || (or) to && (and) and you should be good to go.
Code:
if ((strcmp(argv[2],"OH") != 0) && (strcmp(argv[2],"IH") != 0)) {
printf ("\nIncorrect location code. Valid options: O, OH, IH, OL, or EX\n\n");
exit(0);
}
Of course, you probably want to add more && (strcmp(....) for the rest of the options EX, OL and O. |
|
#5
|
|||
|
|||
|
I hope this helps
Code:
#include <string.h>
int main(int argc, char **argv){
if(argc != 3){ //1 || argc > 2){
printf("\nWARNING: Incorrect usage! Proper usage...\n");
exit(1);
}
printf (argv[2]);
if (!(strcmp(argv[2],"OH") == 0)){
printf ("\n\ndidn't see OH. crap\n\n");
exit(1);
}
printf ("\n\ndetected OH. cool\n\n");
}
|
|
#6
|
||||
|
||||
|
To avoid confusion here, C3PO's 2nd post on this thread was asking what was wrong with this code:
Code:
if (strcmp(argv[2],"OH") != 0 || strcmp(argv[2],"IH") != 0) {
printf ("\nIncorrect location code. Valid options: O, OH, IH, OL, or EX\n\n");
exit(0);
}
Unfortunately he edited the post (and removed all the code) right when I posted my reply. ![]() |
|
#7
|
|||
|
|||
|
Uhm if you put argv[2] wouldn't that just be comparing the character in argv index 2? (so wouldnt it be only 1 char)
|
|
#8
|
||||
|
||||
|
Nope, argv is declared as char ** (or char *argv[] in some cases), which means it's a pointer to pointer to char (or pointer to array of chars if you like). So argv[2] points to the third char array (not 2nd char array, since arrays are zero based in C. However, this array is the second argument passed to the program, since argv[0] is the program name itself). If you want the 2nd char of the 3rd char array, then you could perhaps try argv[2][1].
Hope this helps! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > What's wrong with this simple IF statement? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|