Answer to Question #192762 in C++ for shuvo

Question #192762

• Write a function called by main() named guessNumber that does not take any arguments but returns a number in the range from 0 to 99. This function validates the range of the guessed number. This function is repeatedly called until the guess is within the range from 0-99. This function calls the following function to get an integer value.

• Write a function called by guessNumber() named getInt that takes one argument of type string. It is a prompt to get number from the user. This function returns an integer. This function handles the nonnumerical input. 1

• Finally write the main() function where the game is played. The main Creates the random number from 0 to 99. guess outside the loop. It calls the guessNumber() function for the number guessed and stores it in an int variable called guess. The loop runs as long as the guess is not equal to the computer generated random number.


1
Expert's answer
2021-05-15T13:47:00-0400
#include <iostream>
#include <string>
#include <time.h>       /* time */
using namespace std;


int guessNumber();
int getInt(string message);


//Finally write the main() function where the game is played. 
int main(){
	//initialize random seed
	srand (time(NULL));
	//generate secret number between 0 and 99
	//The main Creates the random number from 0 to 99. guess outside the loop. 
	int secretNumber = rand() % 100;
	int guess=-1;
	while(secretNumber!=guess){
		//It calls the guessNumber() function for the number guessed and stores it in an int variable called guess. 
		//The loop runs as long as the guess is not equal to the computer generated random number.
		guess=guessNumber();
		
		if (secretNumber<guess){
			cout<<"The secret number is lower\n\n";
		}
		if (secretNumber>guess){
			cout<<"The secret number is higher\n\n";
		}
	}
	cout<<"The guess is equal to the computer generated random number.\n\n";
	system("pause");
	return 0;


}


//Write a function called by main() named guessNumber that does not take any arguments but returns a number in the range from 0 to 99. 
//This function validates the range of the guessed number. 
int guessNumber(){
	int number=-1;
	//This function is repeatedly called until the guess is within the range from 0-99. 
	while(number<0 || number>99){
		number=getInt("Enter a number [0-99]: ");
	}
	//This function calls the following function to get an integer value.
	return number;
}
	
//Write a function called by guessNumber() named getInt that takes one argument of type string. 
//It is a prompt to get number from the user. 
//This function returns an integer. 
//This function handles the nonnumerical input. 
int getInt(string message){
	int value=-1;
	bool flag=false;
	while(!flag){
		cout<<message;
		cin>>value;
		if(cin.fail()){
			cin.clear();
			cin.ignore(1000,'\n');
		}else{
			flag=true;
		}
	}
	return value;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment