Thread: Guessing game - loop counting

  1. #1
    Registered User Stargate476's Avatar
    Join Date
    Nov 2009
    Location
    NJ/USA
    Posts
    2

    Guessing game - loop counting

    Hey guys. Basically I am writing a program with a menu to select various games. Then write the code for the individual games. Oh and this is not a homework assignment. More of a practice/boredum project. Relatively new to programming but trying to learn

    Anyway, I am wondering how do I implement a check to see how many times the user entered a guess and if it exceeds it, move onto the next do loop. I will post part of the code for the guessing game portion of my program as it stands now. I tried to use while loops but I am not sure I used them write because it messed up the output and didnt do what I wanted. It does work now the way it is. Just trying to put the checks in.

    Any Suggestions?

    Code:
        do
        {
            num[1]=rand()%10+1;
            num[2]=rand()%100+1;
            num[3]=rand()%1000+1;
            num[4]=rand()%10000+1;
            num[5]=rand()%100000+1;
            do
            {
    
                cout << "\nPick a number between 1 and 10: ";
                cin >> guess;
    
                if (guess>num[1])
                {
                    cout << "\nToo High. Guess Again. ";
                }
                else if (guess<num[1])
                {
                    cout << "\nToo Low. Guess Again. ";
                    tries++;
                }
                if (guess==num[1])
                {
                    cout << "\nCongratulations, you guessed the answer correct\n";
                }
    
            }
            while (guess!=num[1]);
    
            do
            {
                cout << "\nPick a number between 1 and 100: ";
                cin >> guess;
                if (guess>num[2])
                {
                    cout << "\nToo High. Guess Again. ";
                }
                else if (guess<num[2])
                {
                    cout << "\nToo Low. Guess Again. ";
                }
                if (guess==num[2])
                {
                    cout << "\nCongratulations, you guessed the answer correct\n";
                }
            }
            while (guess!=num[2]);
    
            .... goes on  like that till num[5]
    
            cout << "\nWould you like to play again? (y|n) \n";
            contin=getch();
    
        }
    Last edited by Stargate476; 11-25-2009 at 08:33 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Every time you do a cin to get a guess, increment a counter. When the counter exceeds your limit, leave the loop.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User Stargate476's Avatar
    Join Date
    Nov 2009
    Location
    NJ/USA
    Posts
    2
    thanks man. didn't even think of putting the check in the while statement after each do loop. Works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. game loop
    By X PaYnE X in forum Windows Programming
    Replies: 6
    Last Post: 02-15-2005, 01:33 PM
  3. Guessing Number Game Problem
    By cgod in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 07:05 AM
  4. Guessing Game Problems
    By Undeadenemy in forum C Programming
    Replies: 7
    Last Post: 05-30-2003, 11:47 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM