Help: prevent program from crashing by entering non numerical value.

This is a discussion on Help: prevent program from crashing by entering non numerical value. within the C Programming forums, part of the General Programming Boards category; When I enter a non-numerical value, the program crashes. How do I stop this? Also, my error msg does not ...

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

    Help: prevent program from crashing by entering non numerical value.

    When I enter a non-numerical value, the program crashes. How do I stop this? Also, my error msg does not display when I hit option other than 1 or 2 (><)
    Here is the code:

    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 rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    You need to flush the input buffer but before that you should be checking if the input is valid anyhow.

    see this and related articles in the FAQ

    The second problem should be really easy to fix, maybe its a bit early in the morning but i can't spot a reason why that should not work as it is

    Personally i would use a switch instead of the if/else there anyway, keep the default free for your error message
    Last edited by rogster001; 09-23-2010 at 03:34 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Found this as well

    Getting Interactive Input

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add this:
    Code:
    getchar();
    right after the scanf() line of code. That will pull off the newline that scanf() leaves behind on the keyboard buffer. Leaves the input stream "cleaned" if the user has not run wild.

    Code:
    fgets(mycharArray, sizeof(mycharArray), stdin);
    is a much more robust method. Then you can test mycharArray[0] -'0' for your int's value. fgets() doesn't allow buffer overflows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. crashing program, help
    By xniinja in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2010, 03:57 PM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 03:50 PM
  3. Program crashing on free() with an invalid pointer message
    By skreaminskull in forum C Programming
    Replies: 6
    Last Post: 01-23-2009, 04:10 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. How to prevent exiting from running program
    By scorpio_IITR in forum Linux Programming
    Replies: 5
    Last Post: 01-18-2004, 03:15 AM

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