# Thread: Need the best solution

1. ## Need the best solution

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. 2. Is the problem "my prompt displays more than once when they get it wrong"? If so, the answer is "don't print the prompt/get input more than once".

Have you traced out which lines of your program execute in the case of incorrect input? 3. yes. When the user is prompted to input a degree between 0 and 212, lets say they enter "-17". The next line that prints is "Please enter a number above 0", then the user is prompted again to enter a number between 0 and 212. But if they enter a number higher than 212 or lower than 0, I get an error message. I want to figure out a better way to prevent this from happening. My first thought is to use a switch function, but I'm not sure how best to do that. 4. What do you mean "I get an error message"? 5. Your problem is not the conditional (switch/case, or if-statements), your problem is the loop. Your program gets input, and if it's wrong, gets input again. But the second time it gets input, it's already inside your if/else block, and the only thing that can get it to go back is a loop - and you don't have a loop for that situation.

What I would do is have a loop that executes until your input is correct, and inside that loop the only thing you do is get input, and check it. If it's valid input, you break out of the loop and then process the input. 6. I tried this, but it didn't work. The program just ignored my loop and continued executing.
Code:
```	do{

printf("Enter any Fahrenheit temperature above 0 and below 212 degrees: ");
scanf("%f", &fahrenheit);
newline();

while(fahrenheit > 212 && fahrenheit < 0){
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();
}
}
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');``` 7. Originally Posted by ginom71 I tried this, but it didn't work. The program just ignored my loop and continued executing.
Code:
```	do{

printf("Enter any Fahrenheit temperature above 0 and below 212 degrees: ");
scanf("%f", &fahrenheit);
newline();

while(fahrenheit > 212 && fahrenheit < 0){
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();
}
}
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');```
Tell me a number that is both greater than 212 and less than 0 8. hehe. Syntax error on my part, must have had a brainfart. Thanks for pointing that out! 9. Originally Posted by ಠ_ಠ Tell me a number that is both greater than 212 and less than 0
Code:
```                int fahrenheit = -12;
while((unsigned)fahrenheit > 212 && fahrenheit < 0){``` --
Mats 10. Originally Posted by matsp Code:
```                int fahrenheit = -12;
while((unsigned)fahrenheit > 212 && fahrenheit < 0){``` --
Mats
lol, you cheater 11. Originally Posted by ginom71 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:
What a bizarre definition of "incorrect." If the number is lower than the freezing point of water in Celsius, it's incorrect, or if it is greater than the boiling point of water in Fahrenheit, it's incorrect? What on Earth is that? Do you think that temperatures lower than freezing or higher than boiling don't exist?

So, fire is incorrectly hot and the universe is due for a core dump? 12. Originally Posted by brewbuck So, fire is incorrectly hot and the universe is due for a core dump?
Yes!  Popular pages Recent additions 