C++ || Random Number Guessing Game Using Srand, Rand, & Do/While Loop

This is a simple guessing game, which demonstrates the use of srand and rand to generate random numbers. This program first prompts the user to enter a number between 1 and 1000. Using if/else statements, the program will then check to see if the user inputted number is higher/lower than the pre defined random number which is generated by the program. If the user makes a wrong guess, the program will re prompt the user to enter in a new number, where they will have a chance to enter in a new guess. Once the user finally guesses the correct answer, using a do/while loop, the program will ask if they want to play again. If the user selects yes, the game will start over, and a new random number will be generated. If the user selects no, the game will end.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

```Srand Rand Modulus Ctime Do/While Loop```

NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.

``` I'm Thinking of a Number Between 1 and 1000... C++ // ============================================================================ // Author: Kenneth Perkins // Date: Jan 20, 2012 // Taken From: http://programmingnotes.org/ // File: guessingGame.cpp // Description: Demonstrates a simple random number guessing game // ============================================================================ #include <iostream> #include <ctime> #include <iomanip> using namespace std; int main() { // declare & initialize variables char playAgain = 'y'; int userInput = 0; int numGuesses = 0; // Seed the random number generator with the current time so // the numbers will be different every time the program runs srand(time(NULL)); int randomNumber = rand() % 1000+1; // display directions to user cout << "I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.\n\n"; do{ // this is the start of the do/while loop // get data from user cin >> userInput; // increments the 'numGuesses' variable each time the user // gets the guess wrong ++numGuesses; // if user guess is too high, do this code if(userInput > randomNumber) { cout << "Too high! Think lower.\n"; } // if user guess is too low, do this code else if(userInput < randomNumber) { cout << "Too low! Think higher.\n"; } // if user guess is correct, do this code else { // display data to user, prompt if user wants to play again cout << "You got it, and it only took you " << numGuesses <<" trys!\nWould you like to play again (y/n)? "; cin >> playAgain; // if user wants to play again then re initialize the variables if(playAgain == 'y'|| playAgain =='Y') { // creates a line seperator if user wants to enter new data cout<<endl; cout.fill('-'); cout<<left<<setw(30)<<""<<right<<setw(30)<<""<<endl; numGuesses = 0; cout << "\n\nMake a guess (between 1-1000):\n\n"; // generate a new random number for the user to try & guess randomNumber = rand() % 1000+1; } } }while(playAgain =='y' || playAgain =='Y'); // ^ do/while loop ends when user doesnt select 'Y' // display data to user cout<<"\n\nThanks for playing!!"<<endl; return 0; }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jan 20, 2012//    Taken From: http://programmingnotes.org///    File:  guessingGame.cpp//    Description: Demonstrates a simple random number guessing game// ============================================================================#include <iostream>#include <ctime>#include <iomanip>using namespace std; int main() { // declare & initialize variables char playAgain = 'y'; int userInput = 0; int numGuesses = 0; // Seed the random number generator with the current time so     // the numbers will be different every time the program runs srand(time(NULL)); int randomNumber = rand() % 1000+1;  // display directions to user cout << "I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.\n\n"; do{ // this is the start of the do/while loop  // get data from user cin >> userInput; // increments the 'numGuesses' variable each time the user // gets the guess wrong ++numGuesses; // if user guess is too high, do this code if(userInput > randomNumber) { cout << "Too high! Think lower.\n"; }  // if user guess is too low, do this code else if(userInput < randomNumber) { cout << "Too low! Think higher.\n"; }  // if user guess is correct, do this code else {    // display data to user, prompt if user wants to play again cout << "You got it, and it only took you " << numGuesses         <<" trys!\nWould you like to play again (y/n)? "; cin >> playAgain;  // if user wants to play again then re initialize the variables if(playAgain == 'y'|| playAgain =='Y') { // creates a line seperator if user wants to enter new data cout<<endl; cout.fill('-'); cout<<left<<setw(30)<<""<<right<<setw(30)<<""<<endl;  numGuesses = 0; cout << "\n\nMake a guess (between 1-1000):\n\n"; // generate a new random number for the user to try & guess randomNumber = rand() % 1000+1; } } }while(playAgain =='y' || playAgain =='Y');     // ^ do/while loop ends when user doesnt select 'Y'   // display data to user cout<<"\n\nThanks for playing!!"<<endl;          return 0;}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

`I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.`

``` 67 Too low! Think higher. 500 Too low! Think higher. 700 Too high! Think lower. 600 Too low! Think higher. 680 Too high! Think lower. 650 Too low! Think higher. 660 Too low! Think higher. 670 You got it, and it only took you 8 trys! Would you like to play again (y/n)? y ------------------------------------------------------------ Make a guess (between 1-1000): 500 Too low! Think higher. 600 Too low! Think higher. 700 Too low! Think higher. 900 Too high! Think lower. 800 Too high! Think lower. 760 Too high! Think lower. 740 Too high! Think lower. 720 Too high! Think lower. 700 Too low! Think higher. 710 Too high! Think lower. 705 Too high! Think lower. 701 Too low! Think higher. 702 Too low! Think higher. 703 Too low! Think higher. 704 You got it, and it only took you 15 trys! Would you like to play again (y/n)? n ```

`Thanks for playing!!`

Was this article helpful?
👍 YesNo

2 Responses to C++ || Random Number Guessing Game Using Srand, Rand, & Do/While Loop

1. taylor says:

what if the person goes out of bounds will there be an error message telling them they need to enter again

2. In C# is much easier