Thread: Issue With Looping

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    2

    Issue With Looping

    Hello World


    I am new to C, and I am very confused with what is going on in my program. I am sure that people have asked this before, but I am looking around and can't find anything on how to fix it. Any thoughts? Thanks!


    Code:
    // store starting variables
        char buffer[2];
        int numPlayers;
    
    
        // print title
        printf("*------------------------------------*\n");
        printf("|                                    |\n");
        printf("|                Wheel               |\n");
        printf("|                 of                 |\n");
        printf("|               Fortune              |\n");
        printf("|                                    |\n");
        printf("*------------------------------------*\n");
    
    
        printf("\n\nHow many players are there? (1-4 recommended): ");
        fgets(buffer, sizeof(buffer) + 1, stdin);
        fflush(stdin);
        numPlayers = atoi(buffer);
        
        while(numPlayers <= 0 || numPlayers > 10) {
            printf("\nThat amount can't play. Try again: ");
            fgets(buffer, sizeof(buffer) + 1, stdin);
            fflush(stdin);
            numPlayers = atoi(buffer);
        }
    
    
        printf("%d\n", numPlayers);
    
    
        return 0;
    OUTPUT:
    Issue With Looping-capture-png
    Last edited by Michael Ziluck; 09-29-2015 at 12:26 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What issue are you facing?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    2
    I updated my post. I forgot to post the output.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    fflush(stdin) is wrong. fflush() is only valid for use on output streams. Use one of the alternatives shown in this link.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >fgets(buffer, sizeof(buffer) + 1, stdin);
    fgets isn't safe if you pass incorrect parameters. sizeof(buffer) + 1 evaluates to one past the size of the array called buffer.

    Buffer is declared as:
    char buffer[2];

    This isn't big enough for most reasonable inputs even, like negative numbers, or double digits like "10". Remember that strings are terminated with zero characters ( '\0' ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looping issue
    By PunchOut in forum C Programming
    Replies: 6
    Last Post: 10-02-2011, 12:01 PM
  2. looping issue
    By aadil7 in forum C Programming
    Replies: 9
    Last Post: 01-13-2011, 10:20 PM
  3. Do-While looping issue
    By atom.sk in forum C Programming
    Replies: 7
    Last Post: 12-19-2010, 02:02 PM
  4. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  5. Looping Issue
    By stormy in forum C Programming
    Replies: 5
    Last Post: 01-24-2006, 02:35 AM

Tags for this Thread