Answer to Question #192764 in C++ for shuvo

Question #192764

7. The program should contain the following functions.

• Write a function to print the introduction to the game. The call to this function should be the first line of your main() function. This function does not take any arguments and does not return a value. The computer is a player here. It tells the user to guess a number from 0 to 99 and it will tell how many of those digits from the guess appear in its secret number.

• Write a function called by main() named digitMatches that takes two arguments, the number computer generated and the player’s guess, both of type int and returns an integer, the number of matching digits. It doesn’t display the matching digits, it just lets the player know how many digits match.


1
Expert's answer
2021-05-15T17:53:35-0400
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
 
void Menu();
int DigitMatches(int guess, int gener);
 
int main()
{
	Menu();
	srand(time(0));
	int generate = rand() % 100;
	int guess = 0;
	while (true)
	{
		cout << "Enter a number: ";
		cin >> guess;
		if (guess > 99 || guess < 1)
		{
			cout << "Please enter number in range 1-99!" << endl;
			continue;
		}
		if (guess == generate)
		{
			cout << "You won! It is correct number!" << endl;
			break;
		}
		cout << "Number of number matches is: " << DigitMatches(guess, generate) << endl;
	}
	return 0;
}
 
void Menu()
{
	cout << "Hello. This is the game where you should guess the number which was generated by computer 0-99" << endl;
	cout << "Let's play." << endl;
}
 
int DigitMatches(int guess, int gener)
{
	int g1, g2;
	if (gener < 10)
	{
		g1 = g2 = gener;
	}
	else
	{
		g1 = gener / 10;
		g2 = gener % 10;
	}
	int gu1, gu2;
	if (guess < 10)
	{
		gu1 = gu2 = guess;
	}
	else
	{
		gu1 = guess / 10;
		gu2 = guess % 10;
	}
	if (gener < 10)
	{
		if (g1 == gu1 || g1 == gu2) return 1;
		else return 0;
	}
	else
	{
		if ((g1 == gu1 && g2 == gu2) || (g1 == gu2 && g2 == gu1)) return 2;
		if (g1 == gu1 || g1 == gu2 || g2 == gu1 || g2 == gu2) return 1;
	}
	return 0;
}

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