-
Help: strcmp
Ok lets say
name is a char and I typed Bruce
what value would I get?
strcmp(name,"Bruce")
1 or 0?
and if it is one explain why this code source doesn't work
Code:
#include <stdio.h>
#include <stdlib.h>
main()
{ char name[21];
int age;
puts("Are you the chosen one?");
printf("Please ENTER your first name: ");
gets(name);
printf("Please enter your age: ");
scanf("%d", &age);
if (age >= 18 && strcmp(name,"Bruce") == 1)
printf("You are the CHOSEN ONE.\n");
else
puts("Sorry your not the chose one.");
fflush(stdin);
getchar();
}
-
strcmp returns 0 if the two strings are equal.
-
-
And using gets() and fflush(stdin) will get you something else as well.
Read the FAQ.
-
Just remember it like this:
Code:
if(strcmp(name, "AnswerKey") == 0){
TRUE MATCH
}
if(strcmp(name, "ANSWER")){
FALSE MATCH
}
-
I would recommend the latter as:
Code:
if(strcmp(name, "ANSWER") != 0){
/* FALSE MATCH */
}
since it reminds the reader that strcmp() does not return a boolean value.