# Random Number Guessing Game

Printable View

• 07-25-2012
iMattbckr
Random Number Guessing Game
Im a beginner, and I have a problem dealing with random number game.
Im post to allow the user to have 10 tries before the game exits or restarts with another random number, but im having trouble figuring out how to do that.
Also, ive been trying to make a fuction for the code but its not coming out the way i planned.
Any help or tips will do, thanks. =]

Code:

```/*  * File:  main.cpp  * Author: Matthew  *  * Created on July 25, 2012, 12:55 AM  */ #include <iostream> #include <ctime> #include <iomanip> #include <cstdlib> using namespace std; int randomNum(int guess) {     srand(time(0));     int rnum= rand()%5;     char again = 'y';     do     {         if (rnum < guess)         {             cout<<"Too low, Try again. ";             cin>>guess;         }         else if (rnum > guess)         {             cout<<"Too high, Try again. ";             cin>>guess;         }         else if ( rnum == guess)         {             cout<<"You have found the random number!";         }         else         {             cout<<"Would you like to play again? (y or n)"<<endl;             cin>>again;         }     }while (again = 'Y' || again = "y");     } int main(int argc, char** argv) {         srand(time(0));     int guess;     cout<<"I have a number between 1 and 1000."<<endl;     cout<<"Can you guess the number?"<<endl;     cout<<"You will have a maximum of 10 guesses."<<endl;     cout<<"Please try your first guess and good luck!"<<endl<<endl;     cin>>guess;     randomNum(guess);         return 0; }```
• 07-25-2012
Matticus
Quote:

Im post to allow the user to have 10 tries before the game exits or restarts with another random number...
Something like this, perhaps?

Code:

```int maxGuess = 0; do {     // code     ++maxGuess; } while(maxGuess < 10);```
Also, in this line:

Code:

`while (again = 'Y' || again = "y");`
- You should be using the relational operator (==) instead of the assignment operator (=)
- You want lowercase y to be in single quotes, not double quotes
• 07-25-2012
iMattbckr
Quote:

Originally Posted by Matticus
Something like this, perhaps?

Code:

```int maxGuess = 0; do {     // code     ++maxGuess; } while(maxGuess < 10);```

i tried this and it worked.. thanks :)
So this is what i now have

Code:

```#include <iostream> #include <ctime> #include <iomanip> #include <cstdlib> using namespace std; int randomNum(int guess, int maxGuess) {     srand(time(0));     int rnum= rand()%5;     char again = 'y';       do     {         if (rnum > guess)         {             cout<<"Too low, Try again. ";             cin>>guess;         }         else if (rnum < guess)         {             cout<<"Too high, Try again. ";             cin>>guess;         }         else if ( rnum == guess)         {             cout<<"You have found the random number!";         }         else         {             cout<<"Would you like to play again? (y or n)"<<endl;             cin>>again;         }++maxGuess;     }     while (maxGuess < 10);     } int main(int argc, char** argv) {             int guess;  int maxGuess=0;     cout<<"I have a number between 1 and 1000."<<endl;     cout<<"Can you guess the number?"<<endl;     cout<<"You will have a maximum of 10 guesses."<<endl;     cout<<"Please try your first guess and good luck!"<<endl<<endl;     cin>>guess;     randomNum(guess,maxGuess);         return 0; }```

Quote:

Originally Posted by Matticus
Also, in this line:

Code:

`while (again = 'Y' || again = "y");`
- You should be using the relational operator (==) instead of the assignment operator (=)
- You want lowercase y to be in single quotes, not double quotes

but i had to put it in place of the while loop that would restart the game... and i still want to restart the game.. any suggestions??
• 07-25-2012
monkey_c_monkey
Also, make sure it's
Code:

`int rnum= (rand()%1000) + 1;`
since you want the random number between 1 to 1000.

basic mod operator is: any integer % divisor will give you a range b/t 0 to (divisor - 1).
• 07-25-2012
iMattbckr
yeah i got that.. i just had it low to test the if statements easyer...

do you have any suggestions on how I could have the user select whether or no they would like to play again??
i know i would have to use a do while statement just don't know where to put it....
• 07-25-2012
Matticus
Quote:

but i had to put it in place of the while loop that would restart the game... and i still want to restart the game.. any suggestions??
Use nested "while" loops. The outer loop will deal with restarting the game, and the first thing it would do is pick a random number. The inner loop will deal with ten guesses. When the inner loop ends (10 guesses were made or the correct number was guessed), and the user wishes to continue, execution will jump back to the first line of the outer loop (generating a new random number) and start the inner loop (guessing part) over again.
• 07-25-2012
iMattbckr
Quote:

Originally Posted by Matticus
Use nested "while" loops. The outer loop will deal with restarting the game, and the first thing it would do is pick a random number. The inner loop will deal with ten guesses. When the inner loop ends (10 guesses were made or the correct number was guessed), and the user wishes to continue, execution will jump back to the first line of the outer loop (generating a new random number) and start the inner loop (guessing part) over again.

would i create that with two do while loops?
• 07-25-2012
Matticus
Sure, that would work. Have you ever practiced with nested loops?
• 07-25-2012
Elysia
iMattbckr, are you familiar with flow charts? If you aren't, then read up about them and do your logic in a flowchart. I strongly recommend you do the logic for your project in a flowchart, then translate that to code.
Matticus, don't hand out solutions, especially problems regarding logic. This is a basic skill every programmer must master, and the best way to do is to practice.
• 07-25-2012
iMattbckr
no im not familiar with flow charts... would i beable to find a link on this site or could you direct me to one?
• 07-25-2012
iMattbckr
Just a question can you do this game with fuctions?
because i wasnt using functions i would be finished already, but since im taking a summer class and we are currently learning them (at a rapid rate) i was assuming i would have to use a function of some sort...
• 07-25-2012
laserlight
Quote:

Originally Posted by iMattbckr
no im not familiar with flow charts... would i beable to find a link on this site or could you direct me to one?

You can search the Web.

Quote:

Originally Posted by iMattbckr
Just a question can you do this game with fuctions?

Yes.

Quote:

Originally Posted by iMattbckr
because i wasnt using functions i would be finished already, but since im taking a summer class and we are currently learning them (at a rapid rate) i was assuming i would have to use a function of some sort...

The part that you appear to be having trouble with is the logic rather than how to write/use a function, hence the suggestion of a flow chart. As such, I doubt that you would be finished already either way ;)
• 07-26-2012
iMattbckr
so would this be logic...

1: start
2: get random number for user to guess (rnum)
3: ask user to input a number (guess)
4: determine if numbers are same (guess = rnum)
5: if numbers are same congradulate!
6: ask if they would like to play agian ( y or no)
7: yes -> go back to step 2
8: no -> stop

9: if guess is less than rnum
10: ask user for higher guess

11: if guess is greater than rnum
12: ask user for lower guess

13: do either step 9 or 11 untill rnum and guess equal

14: however if guess exceeds 10 inputs
15: notify user of tries and stopping inputs
16: ask user if they would like to try agian (Y or N)

17: Y -> go back to step 2
18: N -> stop program
• 07-26-2012
iMalc
You aren't gaining anything by passing maxGuess in to randomNum, you may as well just make that a local variable to the randomNum function. Of course you could pass in the number 10 and then decrement that until zero instead (or count up to that using a separate variable), which would actually be somewhat useful.

You should more this line to where you had it in main and delete it from where it is now:
Code:

`srand(time(0));`
• 07-26-2012
Elysia
Quote:

Originally Posted by iMattbckr
no im not familiar with flow charts... would i beable to find a link on this site or could you direct me to one?

Please, search the web for simple stuff like this. At least give it a try before asking about it. You are expected to do at least that much.
If you did so, then clearly state so in your reply to avoid being told to search the web. We are not here to babysit you.