Thread: My program won't stop looping

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    My program won't stop looping

    I am new to programming and I have a problem. The problem is that i have to produce a menu driven program which will allow a user to make a choice from a menu. I would really appreciate it if some one could tell me why if I input a letter instead of a number my program goes into an infinte loop. I have included the offending bit of code as a text file please help me cheers Davie
    Davie

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    David,

    The offender is in here:
    Code:
    printf("\n\n\n\t\tPlease Enter Your Menu Choice:  ");
    
    /* read in valid menu option */
    scanf("%i",&choice);
    
    while((choice==0)||(choice>5))
    {
      printf("\n\n\t\tPlease Enter Menu Between 1 and 5 :  ");
      scanf("%i",&choice);
    }/* end of while */
    At the first scanf(), we type in some letter ('x', for example). scanf() is unable to assign the 'x' from stdin to 'choice' because of the conversion character instructing scanf() to put an integer into 'choice'. So, scanf() returns zero and nothing is moved (the 'x' is still sitting inside stdin).

    When we reach the second scanf(), the same thing happens: scanf() looks at stdin, finds the 'x', can't match it against the conversion character %i, and returns zero. Since the value of 'choice' is never updated, the loop runs forever (as long as choice had a value of zero or greater than five when you started out).

    Now that you know why it doesn't work, I'll let you ponder the solution for a while. Come on back if you're still stuck.
    Jason Deckard

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Solution...

    Use fgets with the stdin stream. It is so much better than scanf. You are just going to have to do a sscanf on the string to get the int out. Something like this:
    Code:
    char line[256];
    int choice;
    
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &choice);
    That is the best input method. scanf also has some other less desirable traits. You're better off sticking with fgets and sscanf.
    1978 Silver Anniversary Corvette

  4. #4
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    When we reach the second scanf(), the same thing happens: scanf() looks at stdin, finds the 'x', can't match it against the conversion character %i, and returns zero. Since the value of 'choice' is never updated, the loop runs forever (as long as choice had a value of zero or greater than five when you started out).
    Deckard is quite right,
    also the range that you are asking the user to input is not being tested for.

    if typed in 1,2,3,4,5 it'll loop forever,
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > if typed in 1,2,3,4,5 it'll loop forever,

    Actually, it wouldn't loop at all, since 1-5 are not 0 or >5.

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

    Cool

    After a very very quick look at the code:

    I was thinking the problem was due to choice being a global variable -- used in both the outer loop and inside the inner loop.

    If the inner loop never gets executed... as stated previously by another guru, the the culprit is in the outer loop.

    Might want to reconsider using global variables. They are not always a bad thing, but maybe in this case.

    Good luck,

    David

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    A couple other things.

    1) Get out of the habit of using void main now, while you're still beginning. main returns an int.

    2) There are better methods to clear the screen than using system. They're detailed in the FAQ.

    3) David's right about global variables - they may seem like they help a lot, but it's good practice to use them as spaingly as possible.

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I pretty much agree with Govt on all of these topics. It is darn near close to a sin to use 'void main', as Govt said. Also, don't use the system command. It could be considered a short cut. Look up the function(s) to follow out this process. And as you make larger and more practical programs, then code is going to get copias. Therefore, globals will be a sloppy style of code and bad practice.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop Program
    By peckitt99 in forum Windows Programming
    Replies: 6
    Last Post: 11-13-2007, 10:22 AM
  2. Early program stop problem
    By bmb_ksu in forum C Programming
    Replies: 9
    Last Post: 02-19-2006, 09:24 AM
  3. Looping Program
    By Melon in forum C Programming
    Replies: 14
    Last Post: 02-02-2003, 10:54 PM
  4. continuing program until ready to stop
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2003, 07:19 PM