A program that can only accept a numerical value?

This is a discussion on A program that can only accept a numerical value? within the C Programming forums, part of the General Programming Boards category; I have made an atm, but when i enter a character it crashes. How do I stop this from happening? ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Question A program that can only accept a numerical value?

    I have made an atm, but when i enter a character it crashes. How do I stop this from happening?


    printf("Please select an option: ");
    scanf("%d", & Option);//It will only accept numbers 1-4

    That is what I'm using for the 4 options the user needs to choose from. If you choose a number that's not 1-4 it gives an error. But a character will crash it.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    RTM,
    man page scanf section 3
    Have you learnt if... [else] ?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Yeah, it fully works with an Int, but I just need to get it to stop crashing if anything other than an Int is entered. I'll look at that page now. Thanks

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You could use fgets() + sscanf()/strtol.
    again don't forget to RTM!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    16
    use %s then throw the var into atoi(). you won't crash and still it'll be numerical-only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 12:55 AM
  2. How to execute other program within c program?
    By KoYoungSuk in forum C Programming
    Replies: 7
    Last Post: 06-07-2010, 05:08 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


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