Reputation: 19
I'm new to programming in C and I'm trying to create a program that when a user inputs their name, they are told what their sexual orientation is randomly. Yet, gcc is erroring because I think I am missing a ; on line 17 which is: if(strcmp(x, arrayNames[i] == 0));. I am not too familiar with strcmp(); but I am reading about it in K&R, just can't get my head around it. Below is the entire code:
int main()
{
char *arrayNames[3]={"Jim", "Bob", "Dave"};
char *arrayOrient[3]={"straight", "gay", "bi"};
char x[100];
srand(time(NULL));
int size;
size = sizeof(arrayNames)/sizeof(arrayNames[0]);
int namesRand = rand() % size;
printf("Please enter your name: ");
scanf("%s", &x[0]);
int i;
for(i = 0; i < size; i++)
{
if(strcmp(x, arrayNames[i] == 0));
{
printf("Hello %s, your orientation is: %s", x, arrayOrient[namesRand]);
break;
}
else
{
printf("Invalid name!");
}
}
return 0;
}
Any tips when using strcmp(); will be welcomed.
Upvotes: 1
Views: 170
Reputation: 51
I think you need to remove the semicolon. No need for a semicolon in an if then statement.
Upvotes: 5
Reputation: 3167
The reason that your code is not compiling is that this is a complete statement
if(strcmp(x, arrayNames[i] == 0))
; // If the strings are the same, do an empty expression.
Obviously the statement should be:
if(0 == strcmp(x, arrayNames[i]))
Now we have a block of code with an else without an if. So the compiler complains.
{
} else // This else doesn't match the if, the if is a single-line statement.
Try this:
if(0 == (strcmp(x, arrayNames[i])))
{
printf("Hello %s, your orientation is: %s", x, arrayOrient[namesRand]);
break;
}
else {
printf("Invalid name!");
}
}
Upvotes: 0
Reputation: 61
Zaibis and Dauterman and Hot Licks are correct. Plus: 1. I have also added newlines to the end of the printf, the "\n". 2. In addition, this macro will make your life easier:
#define strEQ(a,b) (0 == strcmp((a),(b)))
...
if(strEQ(x, arrayNames[i]) {
printf("Hello %s, your orientation is: %s\n", x, arrayOrient[namesRand]);
break;
} else {
printf("Invalid name!\n");
}
This is a pretty strange program, that let's users guess a name but not choose their sexual orientation! And how are they supposed to know which of the three names are acceptable?
Also, you have a potential bug here:
arrayOrient[namesRand]
In that the number namesRand was chosen using the length of the other array, arrayNames, instead of the length of arrayOrient.
Upvotes: 0
Reputation: 6555
When you put an semicolon behind an if
statement as in this case:
(I'm not refering to the wrong placed brace as others did I just want to clarify the use of a semicolon)
if(strcmp(x, arrayNames[i] == 0));
{
printf("Hello %s, your orientation is: %s", x, arrayOrient[namesRand]);
break;
}
else {
printf("Invalid name!");
}
}
The standard says if u don't place braccets for a jump statement (as loops or an if statement) , then the scope is just the next expression.
So what happens is: the if
statement is refering to the semicolon;
what means, if the statement is true, nothing will be executed. As there is no expression associated with the semicolon, so the statement is already invoked, and the embraced scope is executed in any case, undependend of the if
statement.
Upvotes: 0
Reputation: 89509
You have a syntax error. Here is what that line should be:
if(strcmp(x, arrayNames[i]) == 0)
See the extra ")" that you need after "arrayNames[i]
"? Strcmp compares two arguments and returns "0" if the two strings are the same.
Remove the extra ");
" (parents and unnecessary semi-colon) at the end of the "if" line.
Upvotes: 2