C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2009, 04:33 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 23
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.
ginom71 is offline   Reply With Quote
Old 07-02-2009, 04:39 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-02-2009, 04:44 PM   #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   Reply With Quote
Old 07-02-2009, 04:48 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
What do you mean "I get an error message"?
tabstop is offline   Reply With Quote
Old 07-02-2009, 04:53 PM   #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   Reply With Quote
Old 07-02-2009, 05:18 PM   #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   Reply With Quote
Old 07-02-2009, 05:22 PM   #7
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Quote:
Originally Posted by ginom71 View Post
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
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-02-2009, 05:25 PM   #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   Reply With Quote
Old 07-02-2009, 05:29 PM   #9
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by ಠ_ಠ View Post
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
__________________
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   Reply With Quote
Old 07-02-2009, 05:48 PM   #10
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Quote:
Originally Posted by matsp View Post
Code:
                int fahrenheit = -12;
		while((unsigned)fahrenheit > 212 && fahrenheit < 0){


--
Mats
lol, you cheater
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-02-2009, 05:53 PM   #11
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by ginom71 View Post
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?
__________________
"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   Reply With Quote
Old 07-03-2009, 02:51 PM   #12
Registered User
 
Join Date: Sep 2008
Location: Toronto, Canada
Posts: 507
Quote:
Originally Posted by brewbuck View Post
So, fire is incorrectly hot and the universe is due for a core dump?
Yes!
nonoob is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22