Need the best solution

This is a discussion on Need the best solution within the C Programming forums, part of the General Programming Boards category; Hi all, I wrote a program that converts Fahrenheit temperatures to Celsius temps. I would like to have the program ...

  1. #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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean "I get an error message"?

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #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');

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    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
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  8. #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!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by matsp View Post
    Code:
                    int fahrenheit = -12;
    		while((unsigned)fahrenheit > 212 && fahrenheit < 0){


    --
    Mats
    lol, you cheater
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Quote Originally Posted by brewbuck View Post
    So, fire is incorrectly hot and the universe is due for a core dump?
    Yes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R solution?
    By deadhippo in forum C Programming
    Replies: 12
    Last Post: 05-09-2008, 07:36 AM
  2. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 09:50 AM
  3. anyone know the solution?
    By heeroyung in forum C Programming
    Replies: 15
    Last Post: 09-30-2005, 07:46 AM
  4. RFC: C++ "Hit any key to continue..." solution
    By flakier in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2004, 11:56 AM
  5. My Unix/Linux SECURITY SOLUTION - pls read
    By bjdea1 in forum Linux Programming
    Replies: 3
    Last Post: 04-11-2004, 10:28 PM

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