Thread: Input looping......it appears once extra time!?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    Input looping......it appears once extra time!?

    I am doing a program to ask the user to input a char with my menu

    however,if the user input a invalid char,i have to ask him to input again

    this is a part of my program:
    ==============================================

    do {
    printf("Menu:\n");
    printf("(a) Enter two dates, compute the number of days between them\n");
    printf("(b) Enter a date, return its day of the week\n");
    printf("(c) Enter a month and year, produce a calendar for the month\n");
    printf("(q) Quit\n\n");
    printf("Which one do you want to do (a/b/c/q)? ");
    scanf("%c",&choose);
    } while ((choose!='a')&&(choose!='b')&&(choose!='c')&&(cho ose!='q'));
    ==============================================

    and this is the output:
    ==============================================

    Menu:
    (a) Enter two dates, compute the number of days between them
    (b) Enter a date, return its day of the week
    (c) Enter a month and year, produce a calendar for the month
    (q) Quit

    Which one do you want to do (a/b/c/q)? s <---invalid input
    Menu:
    (a) Enter two dates, compute the number of days between them
    (b) Enter a date, return its day of the week
    (c) Enter a month and year, produce a calendar for the month
    (q) Quit

    Which one do you want to do (a/b/c/q)? Menu:
    (a) Enter two dates, compute the number of days between them
    (b) Enter a date, return its day of the week
    (c) Enter a month and year, produce a calendar for the month
    (q) Quit

    Which one do you want to do (a/b/c/q)?
    ==============================================

    as above,the menu appears once extra time.......

    also,it the user input two char, the menu displays two extra times.....

    what is the method to debug this problem?
    is that my while loop problem?
    Last edited by siumui; 11-15-2002 at 08:38 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    oh....
    now i understand why it display extra times.....
    thank!


    how do i use fgets() ?
    I am sorry that i am a beginner and actually I don't learn this function yet.....

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    thx you!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could just do:

    scanf( "%c%*c", &mychar );

    However, the best way to use scanf is to not use^H^H^H^H^H^H^Hwrite a function that will clear out the input stream when you're done, and call it after scanf.

    Something akin to:

    while(fgetc(stdin)!='\n');


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time limit for user input
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 03:01 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. One Extra Input
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-07-2005, 11:51 AM
  4. h/w help
    By helpme in forum C Programming
    Replies: 20
    Last Post: 10-21-2003, 09:36 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM