So, I am working on a Random Number Guessing Game project, and i just realize I didn't need to capitalize all of that, but I am already past it. Moving on.
I have the program working almost as intended but I need some help with a part of it. The project states the following, which I do not understand how to go about doing it.
"The program should use a loop that repeats until the user correctly guesses the random number.
A negative guess should end the loop to mean the user has given up."
Now, I have the first part, but I included it for context. What I don't get is "A negative guess should end the loop to mean the user has given up."
Would this mean a guess outside of the 1-100 range is supposed to close the program out? If so, how would I incorporate that into my program?
I have included the program for assistance in making this work.
Also, I can only use the first 5 chapters of my Intro to C++ book, so advanced terms and such I can not use. I am not even sure I can use the boolean expressions here, just in case, how can I rewrite those booleans to work as intended as well?
Either, or both answers would be appreicated very much. Thank you ahead of time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream> // library that contains basic input output functions
#include <cstdlib> // library that contains random numbers generation functions
#include <ctime> // library that contains time functions.
#include <string> // library to use strings
using namespace std;
int main()
{
bool playAgain = true;
while (playAgain == true)
{
int randomNumber = rand() % 100 + 1;
bool correctGuess = false;
int guess;
int attempts = 1;
srand(time(0));
while (correctGuess == false)
{
if (attempts == 1)
{
cout << "Enter Number : ";
}
else
{
cout << "Enter Number Again : ";
}
cin >> guess;
if (guess == randomNumber)
{
cout << "Congratulation! You have guessed the correct number in " << attempts << " attempts" << endl;
correctGuess = true;
}
else
{
attempts++;
if (guess < randomNumber) //if user entered number is less than random number
{
cout << "Too low, try again!" << endl;
}
else //if user entered number is greater than random number
{
cout << "Too high, try again!" << endl;
}
if (guess )
}
}
//Asking user if he/she want to continue game or not
string choice;
cout << "Press Y to play again or any other key to terminate : ";
cin >> choice;
//if user enters any other key than Y/y, set playAgain flag to false
if (choice != "Y" && choice != "y")
{
playAgain = false;
}
}
return 0; //Successful teermination of program returns zero
| |