Thread: newbie Q. about do-while loop

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    13

    newbie Q. about do-while loop

    a warm hello to everybody

    my problem with my first program is that it is looping twice then ends. I would like to loop till user enters a number between 1 and 5.
    So no symbols or letters or numbers out of this range.

    Code:
        int numberOfPlayer;
        int numberOfPlayerTester = 0;
    
    
        printf("Hey to the drinkerman game!\n\n");
        printf("how many people want to get drunk (max 5 players):\n");
        scanf(" %d", &numberOfPlayer);
    
    
        do{
            if(numberOfPlayer != 5 && numberOfPlayer != 4 && numberOfPlayer != 3 && numberOfPlayer != 2 && numberOfPlayer != 1)
            {
            printf("\nSorry, but you have to choose a number between 1 and 5.\nSo how many are you?\n");
            scanf(" %d,", &numberOfPlayer);
            }else
            {
            numberOfPlayerTester = 1;
            }
    
    
        }while(numberOfPlayerTester = 0);

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Take a closer look at line #21. What do you think it does? Before you answer, let me remind you that "a = 0" is totally different from "a == 0".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    13
    perfect greaper, the numbers are doing fine.
    now if user enters a symbol or a letter it prints endless lines of line13 (without stopping)

    has it to do with the fact that "numberOfPlayer" is an int?
    But how can I solve it?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You see, the only input after the loop starts is done inside that if statement, that's why it's behaving that way. You should move the scanf out of that if-else block( but still inside the loop ).

    Also, you have a stray comma in line #13, inside the string. You should remove that.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    13
    Code:
        int numberOfPlayer;
        int numberOfPlayerTester = 0;
    
        do{
            scanf(" %d,", &numberOfPlayer);
            if(numberOfPlayer != 5 && numberOfPlayer != 4 && numberOfPlayer != 3 && numberOfPlayer != 2 && numberOfPlayer != 1)
            {
            printf("\nSorry but you have to choose a number between 1 and 5.\nSo how many are you?\n");
            }else
            {
            numberOfPlayerTester = 1;
            }
    
    
    
    
        }while(numberOfPlayerTester == 0);
    still not going... with letters or symbols still endless lines

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It works fine when I compile and run it. No endless lines or anything.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by kingkoks View Post
    now if user enters a symbol or a letter it prints endless lines of line13 (without stopping)
    If "scanf()" encounters a type it does not expect (such as the user entering a character when an integer is expect), "scanf()" fails, and what caused it to fail remains on the input buffer.

    So in your case, when you enter a character, "scanf()" tries to read it and fails. Code execution continues, the loop is repeated, "scanf()" tries to read the same thing and fails again, and so on. Infinite loop.

    You can check whether "scanf()" successfully read input by checking its return value. "scanf()" returns the number of items successfully read, so if you're trying to read a single integer, "scanf()" will return 1 if successful.

    If "scanf()" was not successful, you need to clear away whatever is on the input buffer. One way is to use a "while" loop as shown here: FAQ > Flush the input buffer - Cprogramming.com

    Alternatively, you could use "fgets()" to read an entire line from the user, and "sscanf()" to retrieve the integer. Failure handling is a bit cleaner with this approach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop structures, please help a newbie
    By Helen Binet in forum Tech Board
    Replies: 5
    Last Post: 12-19-2011, 04:12 AM
  2. newbie: need help using malloc in a loop
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-03-2009, 05:59 PM
  3. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  4. newbie - while loop and printf
    By nickch in forum C Programming
    Replies: 7
    Last Post: 04-14-2005, 02:33 PM
  5. Newbie Loop Question.
    By SlyMaelstrom in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2004, 03:47 PM