Hey, I'm a beginning at coding and I have to make a code that lets you chose if you want to play two games: One where the computer guesses and the second being where you guess the computer's number. I have only made the menu and then first game yet but I'm already having problems. When I play the first game, and tell it that the number it guesses was not the correct number it keeps guessing the same number. Please help in how I can fix that. Here's my code:
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int gameNumber = 0;
int select;
cout << "Welcome to the Guessing Game";
cout << "gameNumber (1: Computer Guesses or 2: User Guesses):" << endl;
cin >> gameNumber;
switch (gameNumber)
{
case 1:
while (true) {
srand(int(time(NULL)));
// lets say I chose a random number between 1 and 100. <-- this code needs to go in the do-while loop.
char answer;
int tries = 0;
do
{
int number_guess = rand() % 100 + 1;
cout << "choose a number between 1 and 100 and I'll guess it." << endl;
cout << "is " << number_guess << " your number?(y/n)" << endl;
++tries; // calculates how many tries it took the computer
cin >> answer;
if (answer == 'y')
cout << "Whoa,I did it in " << tries << " shot(s)! I deserve a pat on the hardware!" << endl;
} while
int number_guess = rand() % 100 + 1;
(answer == 'n'); // continue until the computer is wrong.
while (true){
while (true) {
std::cout << "Would you like to play again (Y/N)? ";
std::cin >> answer;
std::cin.ignore();
if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
}
else {
std::cout << "Please enter \'Y\' or \'N\'...\n";
}
}
if (answer == 'n' || answer == 'N') {
std::cout << "Thank you for playing!";
break;
}
else {
std::cout << "\n\n\n";
}
}