Hi all,
I wrote a program that converts Fahrenheit temperatures to Celsius temps. I would like to have the program account for incorrect input (like a degree higher than 212 or less than 0). This is the code I have written:
Code:
#include <stdio.h>
#include <stdlib.h>
void newline(void);
int main(void)
{
float fahrenheit, celsius; /*values for temperature*/
char choice; /*character for user menu*/
do{
printf("Enter any Fahrenheit temperature above 0 and below 212 degrees: ");
scanf("%f", &fahrenheit);
newline();
if(fahrenheit > 212){
printf("\n\aPlease enter a temperature below 212 degrees.\n");
printf("Enter any Fahrenheit temperature above 0 and below 212 degrees: ");
scanf("%f", &fahrenheit);
newline();
}
else if(fahrenheit < 0){
printf("\n\aPlease enter a temperature above 0 degrees.\n");
printf("Enter any Fahrenheit temperature above 0 and below 212 degrees: ");
scanf("%f", &fahrenheit);
newline();
}
else{
celsius = 5.0 / 9.0 * (fahrenheit - 32);
printf("\n%10s%10s\n%+10.3f%+10.3f\n", "Fahrenheit", "Celsius",
fahrenheit, celsius);
printf("\nContinue? y for yes, n for no\n");
scanf("%c", &choice);
newline();
}
}while(choice != 'n');
system("pause");
return 0;
}
void newline(void) { /*function to eat newline character*/
int c;
do {
c = getchar();
} while(c != '\n' && c != EOF);
}/*end function newline*/
I can get the program to run perfectly, except for situations where users incorrectly input more than once. My idea was to use a switch statement, but can you have an argument in a case statement? For instance,
Code:
case 'fahrenheit > 212':
printf("please enter a number lower than 212");
break;
Any help would be greatly appreciated.