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

  1. #1
    Registered User
    Join Date
    Apr 2010

    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);
    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);

    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(
    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);

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    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."

    "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
    Tomball, TX
    Found this as well

    Getting Interactive Input
    Virtual reality hello world in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Add this:
    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.

    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, 04: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, 05: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, 04:15 AM