Thread: scanf problem

  1. #1
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    Question scanf problem

    I'm used to use the functions getche() or getch() for accepting choices for menus, and I tried to experiment using scanf(). The program example below works fine if I enter an integer, but when I entered any other keys (i.e. A-z) that's not a number, the program crashes. I tried to use isdigit() and isalpha() to detect the error and correct it--but to no avail.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main() {
        int choice;
        do {
            clrscr();
            printf("Binary and Decimal numbers\n\n");
            printf("What do you want to do?\n1. Convert Decimal to Binary\n");
            printf("2. Convert Binary to Decimal\n3. or Exit this program\n");
            printf("Enter number of choice: ");
            scanf("%d", &choice);
            switch(choice) {
                case 1:
                    printf("1");
                    break;
                case 2:
                    printf("2");
                    break;
                case 3:
                    break;
                default:
                    printf("Invalid choice!");
                    choice = 0; // trying to replace a non-integer input but won't work.
                    break;
            }
            getch();
        } while(choice != 3);
        return 0;
    }
    Anyone who can teach me how to detect a non-integer input to an integer-type variable using scanf()?
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well... I'd probably say ditch scanf() and use fgets() to read a string, then convert to whatever you want using sprintf(). That may just be me, though. If you're going to experiment with a new form of input, at least experiment with fscanf().
    Sent from my iPadŽ

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Checking scanf()'s return value is another idea.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    Use scanf to read in a string and then test it and convert it to a number.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by nowChere
    Use scanf to read in a string
    *faints*
    Sent from my iPadŽ

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    What a great explanation dwks! That's what I really longing to know--information about %d or %i.. Thank you!
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM