Hi,
I've written a tiny program to convert between Celsius and Fahrenheit and it has run well with results I expected. Then, I decided to revise it a bit and the problem has arisen which I don't know where from. The problem came when I added the code in bold (see the program below).
Could you please point out where the problem is from? Here's the code which gave wrong stuffs:
What I can't figure out is why it keeps giving the things I highlighted in bold, even though I've entered 'c' or 'C', etc... It just gets stuck there.
Your help is much appreciated!!! I'm trying to teach myself C as the very first language. I don't have programming experience or I've never had any formal computer classes before.
Thanks a lot....
I've written a tiny program to convert between Celsius and Fahrenheit and it has run well with results I expected. Then, I decided to revise it a bit and the problem has arisen which I don't know where from. The problem came when I added the code in bold (see the program below).
Could you please point out where the problem is from? Here's the code which gave wrong stuffs:
Code:
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char unit = 0;
float temperature = 0.0f;
float Celcius = 0.0f;
float Fahrenheit = 0.0f;
printf("You wanna convert to Celcius or Fahrenheit? C for Celcius, F for Fahrenheit: ");
scanf("%c", &unit);
[B]if(!(unit == 'c') || !(unit == 'C') || !(unit == 'f') || !(unit == 'F'))
printf("\nYou've entered the wrong thing! You didn't read the onscreen instructions, did you?");[/B]
else
printf("\nNow enter the temperature you want converted to your chosen unit: ");
scanf("%f", &temperature);
Celcius = (temperature - 32.0f)*(5.0f/9.0f);
Fahrenheit = 1.8f*temperature + 32.0f;
switch (toupper(unit)) {
case 'C':
printf("\nIt's equivalent to %0.3f Celsius", Celcius);
break;
case 'F':
printf("\nIt's equivalent to %0.3f Fahrenheit", Fahrenheit);
break;
}
return 0;
}
Your help is much appreciated!!! I'm trying to teach myself C as the very first language. I don't have programming experience or I've never had any formal computer classes before.
Thanks a lot....