Hi. I am going through beginner exercises on a C++ tutorial. I came across the number guesser, and I was able to make a once-through program very easily. However, when I try to make a "play again" option, I get multiple playtime errors. It plays the first time, but when I guess the number (and for some reason, every time I play the program, that number is 39... is that supposed to happen?), it displays a blank line. Then, whether I input "y" or "n", it briefly displays my "win" output and asks if I want to play again before it automatically exits. I have no idea where to look.
Here is how I set it up:
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time_t(0)); //seed random number generator
int theNumber = rand() % 100 + 1; //random number between 1 and 100
int tries = 0;
int guess;
char again;
cout << "Welcome to Guess My Number!\n\n";//Welcome line
do
{
cout << "You have eight tries to guess the number.\nEnter a guess: ";//Rules
do
{
cin >> guess;//Player input
if (guess > theNumber)
cout << "\nToo high! Guess again: ";
if (guess < theNumber)
cout << "\nToo low! Guess again: ";
++tries;
} while (guess != theNumber,tries <7);//While loop, for wrong guess, and guesses number less than 8
if (guess == theNumber)
{
cout << "\n\nYou got it! You guessed the number in " << tries + 1 << " tries!";//Correct answer
cout << "\nDo you want to play again (y/n)?";//Play again option
cin >> again;
}
if (tries < 7)
{
cout << "\n\nSorry, you took too many tries.";// Wrong answer, too many guesses
cout << "\nDo you want to play again (y/n)?";//Play again option
cin >> again;
}
} while (again == 'y');
return 1;
}
Any help for a total noob will be greatly appreciated.