Thread: atoi

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002


    I am trying to let the user enter only a choice of 1,2or3
    if the user enters a letter I want to give an error message.

    So far my program crashes if the user enters a char.

    I tryed to use atoi but it did not solve the can I solve it.....

    printf("   choice: ");
    scanf("%d", &choice);
    if (isalpha(choice))
          fprintf(stderr,"incorrect choice, try again!\n");

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    dont use scanf. use fgets() and sscanf(). This subject has been covered many times. search the boards.
    basically make an array. fill it with user input with fgets(). use sscanf() to extract an int from the input. use if to make sure int is in range.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with atoi
    By NewbGuy in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 11:55 PM
  2. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  3. Problem with atoi in C
    By garagebrian in forum C Programming
    Replies: 5
    Last Post: 08-24-2004, 01:59 PM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  5. how to use atoi!!!
    By moon in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2002, 09:54 PM