Hi guys, I created a guessing game, but I need to enhance it now to allow myself to keep guessing the number until it is guess correctly, or i choose to quit the program.
// William Lockard
// Assignment 4
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int numGen()
{
srand(time(0));
int randomnum =rand()%100+1;
return randomnum;
}
int main()
{
int guess = numGen();
numGen();
while(1)
{
cout << "Guess a number between 1 and 100 " <<endl;
cin >> guess;
if (guess > numGen())
{
cout << "Your guess is too high. Enter another number " << endl;
}
elseif (guess < numGen())
{
cout << "Your guess is too low. Enter another number " << endl;
}
else
{
cout << "Congratulations that is the correct guess" << endl;
return 0;
}
}
}
This works fine, I am just having trouble using do-while loops and char values to allow the user to input "Quit" in the middle of the code if they wish to quit.
Could someone give me an idea how to go about it? I will attach what I've tried
I'm getting really frustrated, I think I'm having trouble where to put the {} but any help is greatly appreciated. Ive looked at other similar problems, and in my book and I think I'm setting up the do-while correctly
I have no idea what toupper even is, it's not even recognizing it in visual studio. I'm sorry I'm in a basic c++ and havent learned it yet. I wouldn't even have any idea where to put that into my original code. I'm assuming the first line at the begining, but then lines 4-9 need to be added to each step?
I need to have it ask if it wants to quit after every guess.
bool Quit = false;
while(!Quit) {
if (guess > numGen()) {
}
elseif (guess < numGen()) {
}
else {
cout << "Congratulations that is the correct guess" << endl;
return 0;
}
cout << " Do you want to Quit? (q or Q) " << endl; //you figure out where to put this code
cin >> answer;
answer = std::toupper(answer);
if (answer == 'Q')
Quit = true;
}