Thread: Loop fails to include error msg. Also, program crashes when non number entered

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    Loop fails to include error msg. Also, program crashes when non number entered

    How can I fix this so it can include the error msg. Also, the loop fails to include the error msg when an option other than 1 or 2 is entered.

    PHP Code:
    #include <stdio.h>


    int main(void)
    {
        
        
    float original_tempnew_temp;
        
    int option;
        
        
        
    printf("Enter the value you wish to convert.\n");
        
    scanf("%f", &original_temp);
        
        
        
        do{
        
    printf("You entered %.5f. \n"original_temp);
        
    printf("Now, what is the unit of measurment of the value you just entered please type 1 or 2:\n"
                "1. Fahrenheit\n"
                "2. Celsius\n"
    );
        
        
    scanf("%d", &option);
        } while(
    option!=&& option!=2);

        
            if(
    option==1)
            {
            
    printf("You have %.5f degrees Fahrenheit.\n",original_temp );
            
    new_temp = (((original_temp 32) / 9) * 5);
            
    printf("When converted to degrees Celsius you get %.5f degrees Celsius\n"new_temp);
            }
            
            
            else if(
    option==2
            {
            
    printf("You have %.5f degrees Celsius.\n"original_temp);
            
    new_temp = (((original_temp 5) * 9) + 32);
            
    printf("When converted to degrees Fahrenheit you get %.5f degrees Fahrenheit\n"new_temp);
            }
            
            else
            {
            
    printf("Error\n");
            }
        
            
        
    return 
    0;     


  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    I try your code in Mac, there is nothing wrong with it.
    Can you post your error message.

    And, your code is fine as well, only a little problem is that you would better initialize the option before entering the do-while loop

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by lightmanhk View Post
    I try your code in Mac, there is nothing wrong with it.
    Can you post your error message.

    And, your code is fine as well, only a little problem is that you would better initialize the option before entering the do-while loop



    If I enter a letter when prompted to enter the temperature i get this error msg:

    >Process failed to respond; forcing abrupt termination...
    >Exit code: 1

    I want to know how to make program itself state "invalid input" instead of crashing.
    Also, how would I better initialize the option before I enter the do-while loop?

    thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can check the return value of scanf and perform some input error handling. If the return value is not 1 (or n, where n is the number of format specifiers for that scanf call), you then report an input error to the user.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by laserlight View Post
    You can check the return value of scanf and perform some input error handling. If the return value is not 1 (or n, where n is the number of format specifiers for that scanf call), you then report an input error to the user.
    How do I do that?
    thanks

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() is difficult to use for user's input. Your problem is that you need to add a line:

    Code:
    getchar();
    after each line of code with scanf(). Why? Because scanf() leaves the newline in the keyboard buffer, and it goofs up the next scanf() when it gets a char, and it was looking for a number (of any kind).

    fgets() is a much better way to get user input, but you need to know how scanf() works, simply because the books and classes use it so very much.

    Which is bad, because it was designed for formatted input (or "trusted user"), at least. A dumb user can be oh-so-clever at wrecking your program with ........-for-input.

    If you work on your indenting style just a bit, it will improve your ability to troubleshoot your programs. It's good, but the subordinate lines of code inside the do loop, should all be indented beyond the column of the d in do:
    Code:
    do {
       print something               //indented line shows level of subordination
       scanf() some input
       getchar() //remove the newline
       while(some input != some value) { //all subordinate code lines
          make some calculations          //line up inside the loop
          print some message
          scanf() //get more input
          getchar()
       }
    }

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by Adak View Post
    scanf() is difficult to use for user's input. Your problem is that you need to add a line:

    Code:
    getchar();
    after each line of code with scanf(). Why? Because scanf() leaves the newline in the keyboard buffer, and it goofs up the next scanf() when it gets a char, and it was looking for a number (of any kind).

    fgets() is a much better way to get user input, but you need to know how scanf() works, simply because the books and classes use it so very much.

    Which is bad, because it was designed for formatted input (or "trusted user"), at least. A dumb user can be oh-so-clever at wrecking your program with ........-for-input.

    If you work on your indenting style just a bit, it will improve your ability to troubleshoot your programs. It's good, but the subordinate lines of code inside the do loop, should all be indented beyond the column of the d in do:
    Code:
    do {
       print something               //indented line shows level of subordination
       scanf() some input
       getchar() //remove the newline
       while(some input != some value) { //all subordinate code lines
          make some calculations          //line up inside the loop
          print some message
          scanf() //get more input
          getchar()
       }
    }
    wow! thank you for the advice and instruction

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, it could be as simple as:
    Code:
    if (scanf("%f", &original_temp) != 1)
    {
        puts("Invalid input");
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by laserlight View Post
    Well, it could be as simple as:
    Code:
    if (scanf("%f", &original_temp) != 1)
    {
        puts("Invalid input");
        return 0;
    }
    Ahh! I see now. That was easy. thank you

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    Your problem is that you need to add a line: (...) after each line of code with scanf(). Why? Because scanf() leaves the newline in the keyboard buffer, and it goofs up the next scanf() when it gets a char, and it was looking for a number (of any kind).
    Actually, that is not the problem. The concern is legitimate when say, %c is used or the scanf is followed by a fgets or fgetc, but in this case the reading of the numbers with scanf would skip the whitespace left in the input buffer anyway.

    Quote Originally Posted by Adak
    fgets() is a much better way to get user input, but you need to know how scanf() works, simply because the books and classes use it so very much.
    You would still need to know how scanf works in order to use fgets here since the fgets call would be followed by sscanf, which is similiar to scanf. Using fgets will not fix the problem in this case, because if you fail to check for input error with sscanf, you would end up with the same problem when the user enters invalid input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Enter a letter instead of a number, as the temperature, (first prompt), and watch it loop forever.

    Second loop is not affected in that way.

    It's true, we need scanf() (and it's related functions), but WHY do they have to all be SO dodgy? It's probably on a "to do" list, right after "give cartoon characters good orthodontic care", or as Homer Simpson pleaded "When will the car makers build moon-roofs for the portly gentleman?". ROFL!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 07-06-2009, 07:12 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM