In: Computer Science
Exercise3: GuessingGame.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int getRandom(){
srand(time(0)); //seed random number generator
int num = rand() % 100 + 1; // random number between 1
and 100
return num;
}
int getNum(){
int guess;
cout << "Enter a guess between 1 and 100 : ";
cin >> guess;
return guess;
}
int getRes(int g,int n){
if(g>n)
return 1;
else if(g<n)
return -1;
else
return 0;
}
int main()
{
int num, guess, res = 0,ch=1;
while(ch){
num=getRandom();
cout << "Guess My Number Game\n\n";
do
{
guess=getNum();
res=getRes(guess,num);
if (res > 0)
cout <<
"Too high!\n\n";
else if (res < 0)
cout <<
"Too low!\n\n";
else
cout <<
"\nCorrect! You got it\n";
} while (guess != num);
cout<<"Press 1 to play again..0 to exit:
";
cin>>ch;
}
return 0;
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me