I need a c++ code that outputs the following: I have a number between 1 and 1000
ID: 3761923 • Letter: I
Question
I need a c++ code that outputs the following:
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess. ?
500
Too high. Try again. ?
250
Too low. Try again. ?
108
Excellent! You guessed the number! Ahah! You know the secret! Would you like to play again (y or n)? y
I have a number between 1 and 1000. Can you guess my number? Please type your first guess. ?
500
Too low. Try again. ? 950 Too high. Try again. ? 934 Excellent! You guessed the number! You should be able to do better! Would you like to play again (y or n)? n Press any key to continue . . .
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
while (true) {
int random=rand()%1000 + 1;
cout<<"I have a number between 1 and 1000."<<endl;
cout<<"Can you guess my number?"<<endl;
cout<<"Please type your first guess. ?"<<endl;
while (true) {
int ans;
cin>>ans;
if (ans > random) {
cout<<"Too high. Try again. ?"<<endl;
} else if(ans < random) {
cout<<"Too low. Try again. ?"<<endl;
} else {
cout<<"Excellent! You guessed the number! Ahah! You know the secret!"<<endl;
break;
}
}
cout<<"Would you like to play again (y or n)?";
char ch;
cin>>ch;
if (ch == 'n') {
break;
}
}
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.