![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 23
| Need the best solution 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*/
Code: case 'fahrenheit > 212':
printf("please enter a number lower than 212");
break;
|
| ginom71 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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? |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 23
| 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. |
| ginom71 is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| What do you mean "I get an error message"? |
| tabstop is offline | |
| | #5 |
| Super Moderator Join Date: Sep 2001
Posts: 4,680
| 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. |
| sean is offline | |
| | #6 |
| Registered User Join Date: Jul 2009
Posts: 23
| 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');
|
| ginom71 is offline | |
| | #7 | |
| Banned Join Date: Mar 2009
Posts: 533
| Quote:
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ | |
| ಠ_ಠ is offline | |
| | #8 |
| Registered User Join Date: Jul 2009
Posts: 23
| hehe. Syntax error on my part, must have had a brainfart. Thanks for pointing that out! |
| ginom71 is offline | |
| | #9 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: int fahrenheit = -12;
while((unsigned)fahrenheit > 212 && fahrenheit < 0){
![]() -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #10 |
| Banned Join Date: Mar 2009
Posts: 533
| lol, you cheater
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ |
| ಠ_ಠ is offline | |
| | #11 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| Quote:
So, fire is incorrectly hot and the universe is due for a core dump?
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot | |
| brewbuck is offline | |
| | #12 |
| Registered User Join Date: Sep 2008 Location: Toronto, Canada
Posts: 507
| |
| nonoob is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| K&R solution? | deadhippo | C Programming | 12 | 05-09-2008 06:36 AM |
| 'Solution' and 'Project' usage in VC++ | C+/- | C++ Programming | 2 | 01-13-2007 09:50 AM |
| anyone know the solution? | heeroyung | C Programming | 15 | 09-30-2005 06:46 AM |
| RFC: C++ "Hit any key to continue..." solution | flakier | C++ Programming | 11 | 09-02-2004 10:56 AM |
| My Unix/Linux SECURITY SOLUTION - pls read | bjdea1 | Linux Programming | 3 | 04-11-2004 09:28 PM |