Question: In this C++ code I'm suppose to create a guessing the random number game with input validation for the range and to check if the
In this C++ code I'm suppose to create a guessing the random number game with input validation for the range and to check if the user enters a int digit. The game will ask the user if they want to play again if they guess the number. It should also display the amount of tries even if they enter a wrong input. I need help in fixing my code.
#include
using namespace std;
int main(){
int number, guess, count;
char answer;
srand(time(0));
int check;
bool invalid = true;
bool end = true;
bool validation =true;
while(end){
while (invalid){
bool validation = true;
check = 0;
cout<<"Please enter a number between 1-100: "< cin>>guess; for(int i = 0; i < guess.size(); i++){ if(isdigit(guess[i]) != true){ validation = false; count++; continue; } } cout<<"Please enter a real number."< if(validation = false){ count++; } stringstream x(guess); x >> guess; if(guess > 100){ cout <<"out of the range."< count++; } if(guess < 0){ cout<<"out of the range."< count++; } if(check == guess.size() && number > 0 && number <101){ invalid = false; } } if(guess > number){ cout<< "Guess is too big!"< count++; cout< } if(guess < number){ cout<< "Guess is too low!"< count++; cout< } else{ cout<< "Congratulation! You got the number."< cout< } cout>>("Do you want to play again? (Y/N))">>endl; cin>>char; if((char == 'y') || (char == 'Y')){ return 0; } else if((char == 'n') || (char == 'N')){ break; } return 0; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
