In: Computer Science
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they are wrong • Use a loop (WHILE or DO) to allow the program to be repeated by typing in the word “YES” ignore case sensitivity • Ensure that every time you repeat a NEW random number is generated • Increment the appropriate variables upon the following instances: o A win (add 1 to winCount) o A loss (add 1 to lossCount) o New game played (add 1 to playCount) • At the end of the program neatly display the user their stats (Their times played, their win count and their losses)
here is the solution in c++
here is the code
#include <iostream>
#include<string>
using namespace std;
int main()
{ //variables declaration
int win=0,loss=0,total=0,num,n;
string st;
while(1)
{ //generate random number
num=rand()%11+1;
cout<<"Enter guess number:\n";
cin>>n;
//number validation
while(n<1 or n>10)
{
cout<<"Invalid! number Enter again:\n";
cin>>n;
}
//check if guess isequal to n
if(n==num)
{
cout<<"you won!\n";
win=win+1;
total=total+1;
}
else
{
cout<<"Answer is wrong.You loss\n";
loss=loss+1;
total=total+1;
}
//take user choice
cout<<"Enter yes to continue or no to quit:\n";
cin>>st;
if(st=="NO" or st=="no")
break;
}//display the stats
cout<<"Total games:"<<total<<"\n";
cout<<"Total wins:"<<win<<"\n";
cout<<"Total loss:"<<loss<<"\n";
return 0;
}