Public Group

# Guess the Number game - open to suggestions

This topic is 1778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Firstly, Hello everyone, this will be my first time posting here and I'm just getting into programming. I've decided upon c++ as my language and currently am working on a guess the number game, slightly modified from a few I have found online. I would much rather build upon this one program for a little while before moving on just to see all of the features that can be added to some simple little game, so currently I've got the guess the number part working generating a random number based off of the time anywhere between 1 and 100. It also tells you how many times you've guessed, but I would like to add a loop to it so that the user can play the game multiple times without having to close it out each time. There just seems to be a plethora of suggestions on how to go about adding this loop and I'm not sure which is the best for this scenario so I'm posting here in hopes someone may have a good suggestion.

The program has a do while loop already in it and maybe I could incorporate what I want to do inside of that loop, I'm not entirely certain on it, because I just learned about loops the other day and haven't messed with them too much. Any advice is appreciated. Also I'm not sure if this is where I would post this sort of thing, thank you in advance.

//Random Number Guessing Game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
int num, guess, tries = 0; //defines the integers num, guess, and tries
srand(time(0)); //seed random number generator
num = rand() % 100 + 1; // random number between 1 and 100
cout << "Guess My Number Game\n\n"; //displays name of the game
do
{
cout << "Enter a guess between 1 and 100 : "; //displays text, prompting
//the player for a number between 1 and 100
cin >> guess; //this will retrieve the input from the player and proceed
//to the following if statement to determine whether their guess is too
//high or too low.
tries++;
if (guess > num)
cout << "Too high!\n\n";
else if (guess < num)
cout << "Too low!\n\n";
else
cout << "\nCorrect! You guessed it in " << tries << " guesses!\n";
//this displays text letting the player know that they have
//correctly guessed the number and in the amount of tries that it
//took.
} while (guess != num); //this is the end of the do while loop, it is
//nullified by the fact that the guess is now equal to the number generated.

cin.ignore();
cin.get();
return 0;
}


##### Share on other sites

You could use this as a chance to make your own function.   You can move the while loop into the function then have a new loop in the main function that calls this function until the player is done.

##### Share on other sites

Agreed with LennyLen. Also, a double loop is not harmful. Think of the logic of your program: you are going to be asking the user repeatedly for a number until he guesses, and you are going to be doing THAT until the user tells you to quit the game. That's two loops: one for the "enter number" part and one for the "do you want to play again" part.

You could also make it part of the loop, by picking a new random number when the user gets it right (and wants to play again), but that means the main loop now depends on whether you want to allow the user to play more than once. Try to break the work in small chunks of code and working your way up from there to create complex programs.

Also, as an aside, you comment too much. Comments should explain why, not how or what. When we (as programmers in general) see "srand(time(0))" we know the random number generator is being seeded. When we see "while" inside a do..while loop we know that's the termination condition. And so on. Most of your comments are mostly repeating the intent of the code, having an english version of the code interleaved with it is just clutter and does not add useful information (unless you want to get a non-programmer to follow the code, but what's the point of that). Generally comments are used to explain tricky or non-obvious parts of the code, or to document a function or a class, explaining what it does at a high level, what it expects, what it does not do, how it helps people, etc.. and should be used sparingly without repeating yourself Also comments that explain code tend to drift out of sync with the code itself, but you can read all about that everywhere on the internet. It can be hard to get out of this habit if you've started out but it will serve you better in the long run.

##### Share on other sites

Thank you both for the feedback. I'm looking more into functions and am just pacing myself learning only a couple of items per day, but trying to go back and integrate them into this one project. As for the comments, I do see how they would clutter up a program, I only had them in there for my personal reference because I don't know all of the details about some of these functions so maybe I should start up a scratch pad for some of this because the reflection does help me out quite a bit. Currently I am enjoying everything I'm seeing, there is a ton of stuff you can do, it seems almost limitless, eventually I would love to move onto a graphics based game, but now that is going to take some time to build up to.

##### Share on other sites

Hello again, I just wanted to update you guys, I did get this working as intended and even added some additional features to it such as:

- I ended up getting the loop to work by having one do while loop inside of another, I think the proper term used is nested, that was my workaround, I'm finding the more I learn there are several ways to improve upon this code

- difficulty options, you can choose easy, medium, or hard difficulty now

- added score tracking by giving an average number of guesses per game

- it also gives you a number of games played, whether you played 4 easy games,1 medium, 1 hard, etc

I enjoy working on this one project, learning a few new things and coming back to it, I've yet to try this out with functions yet which is my plan for this evening. I am going to add some classes to the whole thing and add some more structure to my code. Structure aside, I'm trying to think of some creative features that I could add to this to get my feet wet, with the intent of eventually making this graphical, which I know i'm a ways off from, does anyone have some suggestions for features that could be added?

##### Share on other sites

does anyone have some suggestions for features that could be added?

• You could add a multiplayer option to the game.
• Add more statistics - average/highest/lowest number of guesses, time played, etc.
• Add random feedback when the game is over based on how well the player performed.
Edited by LennyLen

##### Share on other sites

That's a very good program for a beginner. But it's also very simple and hard to expand further ;)

It's the kind of a mini-game which works perfectly in text mode (console application) and don't really need any graphics.

So I would just suggest you to polish it as much as possible and then move to something more complex ;) Just two quick tips:

- verify the numbers entered by player: you ask for a number between 1 and 100, but you don't really check whether he really gave you such a number

- don't say "Correct! You guessed it in 1 guesses", it should be "1 guess" in this special case ;)

- maybe (for the sake of programming practice) make the answers depend on how close the guess is, try something in the sense of "Very close, just a little bit higher", "You are way off, you must go much higher" etc.

Edited by Tom KQT

##### Share on other sites

does anyone have some suggestions for features that could be added?

• You could add a multiplayer option to the game.
• Add more statistics - average/highest/lowest number of guesses, time played, etc.
• Add random feedback when the game is over based on how well the player performed.

Thank you, those will provide a nice polish to it.

That's a very good program for a beginner. But it's also very simple and hard to expand further ;)

It's the kind of a mini-game which works perfectly in text mode (console application) and don't really need any graphics.

So I would just suggest you to polish it as much as possible and then move to something more complex ;) Just two quick tips:

- verify the numbers entered by player: you ask for a number between 1 and 100, but you don't really check whether he really gave you such a number

- don't say "Correct! You guessed it in 1 guesses", it should be "1 guess" in this special case ;)

- maybe (for the sake of programming practice) make the answers depend on how close the guess is, try something in the sense of "Very close, just a little bit higher", "You are way off, you must go much higher" etc.

I hadn't thought of checking how close they were to the number, that'll be a nice feature to play around with. I wanted to expand as much on this one program as much I could, and then in future games it'll be easier for me to recollect how it was done before. Thank you for the great advice.

1. 1
Rutin
65
2. 2
3. 3
4. 4
5. 5

• 18
• 10
• 30
• 20
• 9
• ### Forum Statistics

• Total Topics
633416
• Total Posts
3011772
• ### Who's Online (See full list)

There are no registered users currently online

×