Hi, I got another problem with my code? I have a small guessing game I made. You guess a number 1-100, and it tells you whether its too high or too low.
Once you guess the number it tells you how many tries it took you to guess the number. But I found a bug where if you type in an invalid character the program just goes haywire.
First off here's my code
The commented part in the middle was my idea of a solution. I couldn't find a way to fix a invalid type input, so I first set out to find a way to fix an input of lets say 105. So I got an idea, but then it would get the number of tries wrong, then I tried something else and it would always ask for a second input even if the first was correct.Code:// Guess My Number
// The classic number guessing game
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
srand(time(0)); // seed random number generator
int upTo
int theNumber = rand() % 100 + 1; // random number between 1 and 100
int tries = 0, guess;
cout << "\tWelcome to Guess My Number\n\n";
do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;
// if ((guess >=100) && (guess <= 0))
// {
//
// while ((guess >=100) && (guess <= 0))
// {
// cout << "Invalid Response\n";
// cout << "Enter another guess: ";
// cin >> guess;
// };
};
if (guess > theNumber)
cout << "Too high!\n\n";
if (guess < theNumber)
cout << "Too low!\n\n";
} while (guess != theNumber);
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
return 0;
}
If someone could help me with this problem, I'd appreciate it.
Thanks,
-Vic