Reputation: 91
I've created a simple guessing game. Pick a number between 0 and 100, depending on the number you enter the program will output hot, Warm, Cold or correct!. The number I've chosen to be the correct answer is 51. My program compiles and runs but always outputs correct! for every value I enter. Thanks for helping if you decide to.
int main(void)
{
int number, answer;
answer = 51;
printf("Enter a number between 0 and 100:\n");
scanf("%d", &number);
if ((number > 51) && (number <= 56) || (number < 51) && (number >= 46))
{
printf("Hot\n");
}
else if ((number > 56) && (number <= 66) || (number < 46) && (number >= 36))
{
printf("Warm\n");
}
else if ((number > 66) && (number <= 100) || (number <36) && (number >= 0))
{
printf("Cold\n");
}
else if ((number > 100) || (number < 0))
{
printf("Error number has to be between 0 and 100 - re run\n");
}
else (number = 51);
{
printf("Correct!\n");
}
system ("pause");
return 0;
}
Upvotes: 0
Views: 85
Reputation: 882596
This little snippet:
else (number = 51);
{
printf("Correct!\n");
}
is actually equivalent to:
else
(number = 51);
//=======================
{
printf("Correct!\n");
}
The first part of that is the else
clause which, if activated, will set number
to 51
.
The second part is totally disconnected from the if
statement section and is a standalone block that will execute no matter what.
If you must put the condition there for readability (and it would be ==
rather than =
), you could do:
else // (number == 51)
{
printf("Correct!\n");
}
As an aside, you could make your code shorter and more readable by starting at the specific and moving to the general, something like (just replacing the if
statement):
if (number == 51)
printf ("Correct!\n");
else if ((number >= 46) && (number <= 56))
printf ("Hot\n");
else if ((number >= 36) && (number <= 66))
printf ("Warm\n");
else if ((number >= 0) && (number <= 100))
printf ("Cold\n");
else
printf ("Error, number has to be between 0 and 100 - re run\n");
}
You don't need to ever check the two ranges (above and below) for "temperature" since the inner ranges have already been checked.
Upvotes: 4
Reputation: 1
In the last else statement, you should either use else if (number == 51) { ... } or
else
{
...
}
Upvotes: 0
Reputation: 176
your code says: if none of the if statements is satisfied, assign 51 to number. and then always print "Correct!\n".
Upvotes: 0
Reputation: 19874
else (number = 51);
{
printf("Correct!\n");
}
can be re-written as
else
{
number = 51;
}
{
printf("Correct!\n");
}
So irrespective of what your number is the printf("Correct!\n");
gets executed.
It can be fixed like
else
{
printf("Correct!\n");
}
Upvotes: 1
Reputation: 6264
remove the condition from the else statement.
int main(void)
{
int number, answer;
answer = 51;
printf("Enter a number between 0 and 100:\n");
scanf("%d", &number);
if ((number > 51) && (number <= 56) || (number < 51) && (number >= 46))
{
printf("Hot\n");
}
else if ((number > 56) && (number <= 66) || (number < 46) && (number >= 36))
{
printf("Warm\n");
}
else if ((number > 66) && (number <= 100) || (number <36) && (number >= 0))
{
printf("Cold\n");
}
else if ((number > 100) || (number < 0))
{
printf("Error number has to be between 0 and 100 - re run\n");
}
else
{
printf("Correct!\n");
}
return 0;
}
Upvotes: 0