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

This is a discussion on Loop fails to include error msg. Also, program crashes when non number entered within the C Programming forums, part of the General Programming Boards category; How can I fix this so it can include the error msg. Also, the loop fails to include the error ...

  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
    21,759
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,759
    Well, it could be as simple as:
    Code:
    if (scanf("%f", &original_temp) != 1)
    {
        puts("Invalid input");
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,759
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 01: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

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