#include #include #include using namespace std; using std::cout; using std::endl; int main() { int x; /* variable to hold our random integer*/ string usIn; int intIn = -1; bool inGood = false; int guesses = 1; srand(time(NULL)); /*seeds random number generator. Do this just once*/ x = rand() % 51; while(intIn != x && guesses <= 5) { inGood = false; while(inGood == false) { cout << "Guess what number x is: "; cin >> usIn; for(int x = 0; x < usIn.size(); x++) { if(usIn[x] < 48 || usIn[x] > 57) { cout << "Bad input, enter a new guess" << endl; inGood = false; break; } else { inGood = true; } } //return //continue if(inGood == false) { continue; } else { intIn = atoi(usIn.c_str()); } } if(intIn != x) { cout << "Wrong! you are "; if(intIn < x) { cout << "too low, try again." << endl; } else if(intIn > x) { cout << "too high, try again." << endl; } } else if(intIn == x) { cout << "You were exactly right! x = " << x << endl; return 0; } guesses++; } cout << "Too many guesses! x = " << x << endl; return 0; }