Create an application, using the following names for the solution and project, r
ID: 3934743 • Letter: C
Question
Create an application, using the following names for the solution and project, respectively: Guessing Game Solution and Guessing Game Project. Save the application in the VB2015Chap05 folder. The application should generate a random integer from 1 through 30, inclusive. It then should give the user as many chances as necessary to guess the integer. Each time the user makes a guess, the application should display one of three messages: “Guess higher”, “Guess lower”, or “Correct. The random integer is x.”, where x is the random integer. The application should also display the number of chances that were required for the user to guess the number. Create a suitable interface, and then code the application. Test the application appropriately.
Explanation / Answer
Following is the C++ code for the application :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand( time(NULL) );
int guess;
int totalChancesTaken = 0;
int randomNumber = rand()%30 + 1; //random number between 1 and 30
while( true ){
totalChancesTaken++;
cout<< "Your Guess: ";
cin >> guess;
if( guess > randomNumber ){
cout << "Guess Lower" << endl;
}
else if( guess < randomNumber ){
cout << "Guess Higher" << endl;
}
else{
cout << "Correct. The random integer is " << randomNumber << "." << endl;
break;
}
}
cout << "Total chances taken : " << totalChancesTaken << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.