guessing game

This is a discussion on guessing game within the C++ Programming forums, part of the General Programming Boards category; hey all, i just have a question about this guessing game code on the site. What exactly is the point ...

  1. #1
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24

    guessing game

    hey all, i just have a question about this guessing game code on the site. What exactly is the point of using else to tell the person there wrong and have "you guess to low/high" on there?...There really both just telling you that your wrong


    Code:
    #include <stdlib.h>
    #include <iostream.h>
    
    int main()
    {
    int number=rand()%100; 
    int
    guess=-1;
    int trycount=0;
    while(guess!=number && trycount<8)
    {
    cout<<"Please enter a guess: ";
    cin>>guess;
    if(guess<number)
    cout<<"Too low"<<endl;
    if(guess>number) cout<<"Too high"<<endl;
    trycount++;
    }
    if(guess==number)
    cout<<"You guessed the
    number";
    else
    cout<<"Sorry, the number was:
    "<<number;
    return 0;
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Make a flow chart of the program. I believe they're telling you "Too low/high" while you you're still in the guessing phase of the game. When you run out of guess is when they'll tell you that you guessed wrong.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Did you run this program???

    The too-high too-low are intermediate results reported inside the while loop. You only get to the else condition after you fail to guess correctly 8 times, then exit the loop (without guessing correctly).

    The logic is a bit screwy, because you test for the correct guess inside the loop and again after exiting the loop. (It's generally a bad idea to test the same thing twice.)

    Oh... I see sean_mackrory already answered...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 10:26 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 01:48 AM
  4. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 04:05 AM
  5. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21