Reputation: 187
This code is functioning almost perfectly but it seems to be ignoring my if else statements. The goal of this program is to only convert temperatures between -100 and 100 but for some reason my code is still converting any number that is entered. It is frustrating because I have tried several different approaches but the program never goes to the else statement and always runs the if statement even if say I entered 10000. Thank you any advice would be appreciated
double converter()
{
float C, F, S;
int input;
printf ("Please enter a temperature in Fahrenheit:");
scanf ("%f", &F);
if (F > -100 || F < 100) {
C = FCR * (F -32);
printf ("%f F ==> %f C ", F, C);
printf ("%f", F);
}
else {
F = pow(C, 3);
printf ("Invalid Fahrenheit temperature.");
}
}
Upvotes: 0
Views: 190
Reputation: 14886
Change || to && and it shall work.
If one side of ||
is true, you enter the if.
And a 1000 is indeed greater than -100
One more note. C is uninitialized. You only assign it a value if the expression in the if evaluates to true. But in else
you use the C when it is not initialized. And always check the return value of scanf
.
Upvotes: 2
Reputation: 521429
Currently your if
condition will accept any Farenheit temperature greater than -100
or less than 100
. Well this means that every temperarure will enter that if
statement. You probably intended to use AND instead:
if (F > -100 && F < 100) {
// logic here
}
Upvotes: 6