Write a c++ program that has the user guess a number between 1 and 100, you program should output correct guess if the user guess the right number, too high if they guess a number higher, too low if the guess a number lower and handle invalid input
Code:
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main() {
int max;
max = 99; //set the upper bound to generate the random number
srand(time(0));
int original = (rand()%max)+1;
int guess = 102;
cout<<"Welcome to the guess game!"<<endl;
cout<<"Enter your guessing number between 1 and
100"<<endl;
while(guess!=original){
while(true){
cout<<"Enter number to guess: ";
cin>>guess;
if(guess>100 || guess<1){
cout<<"Invalid number."<<endl;
cout<<"Enter your guessing number between
1 and 100"<<endl;
}
else
break;
}
if(guess>original)
cout<<"Guessed
number is GREATER."<<endl;
if(guess<original)
cout<<"Guessed
number is LOWER."<<endl;
}
cout<<"CONGRATULATIONS! You guessed the correct
number."<<endl;
return 0;
}
Output:
Hope this helps.
Get Answers For Free
Most questions answered within 1 hours.