Write a number guessing game in which the computer selects a
Solution
Answering 1st question :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
string winningMessages[3] = {\"You won!!!\",\"Congratulations, you guessed it correctly\", \"Correct guess!!!\"};
string losingMessages[3] = {\"Sorry, you lost it\",\"Sorry, allowed attempts are finished\",\"Sorry, good luck next time\"};
string playAgainMessages[3] = {\"Wanna play again? \",\"Wanna try again? \",\"Wanna have another round? \"};
void number_guessing_game(){
int wins = 0;
int losses = 0;
char inp;
while(1){
cout << \"Let the games begin!!! \"<<endl;
int number = rand()%101; //for number in range 0 to 100
bool Won = false;
for(int attemptNo = 1; attemptNo <= 20; attemptNo++ ){
int guess;
cout << \"Attempt \" << attemptNo << \", Your guess: \";
cin >> guess;
if( guess == number ){
Won = true;
break; }
cout << \"Sorry, it is incorrect\" << endl;
}
int msgNo = rand()%3;
if( Won ){ cout << winningMessages[msgNo] << endl; wins++; }
else{ cout << losingMessages[msgNo] << endl; losses++; }
msgNo = rand()%3;
cout << playAgainMessages[ msgNo ];
cin >> inp;
if( inp == \'N\' || inp == \'n\' ){ break; }
}
cout << \"Total number of wins: \" << wins << endl;
cout << \"Total number of losses: \" << losses << endl;
}
int main(){
number_guessing_game();
}
