Answer to Question #192760 in C++ for shuvo

Question #192760

Let us consider a guessing game in which the computer picks a random two- digit number in the range from 0 to 99 and asks the user to guess the number. After each guess, the computer lets the player know how many digits of guessed numbers match the correct number. If the guessed number matches the correct number, the computer lets the player know the number of guesses the player made.The goal of testing is to make the function fail as soon as possible so that, you can correct it immediately. Please do not use strings int his program. You should be able to do digit matches perfectly without using a string. The following is the example log of the execution:

Try to guess my number form 0 - 99 and I will tell you how many digits of your guess match my number. Your guess?

50 Incorrect (hint: 0 digits match)

your guess?

73 Incorrect (hint: 0 digits match)

Your guess?

yes That’s not an integer. Try again

Your guess?

79 Incorrect (hint: 0 digits match)

Your guess?

300 Invalid number. Try again!

Your guess?

34 Incorrect (hint: one digit matches)

Your guess?

36 Incorrect (hint: 0 digits match)

Your guess?

48 Incorrect (hint: 2 digits match)

Your guess?

84 Congratulations! You took 9 guesses


1
Expert's answer
2021-05-15T07:32:40-0400
#include<iostream>
#include<conio.h>
#include <stdlib.h>
#include <time.h>


using namespace std;


int main() {
	srand(time(NULL));
	cout << "Try to guess my number form 0 - 99 and I will tell you how many digits of your guess match my number. ";
	int a, i = 0, b = rand() % 100;
	do {
		cout << "Your guess?";
		bool st = true;
		cin >> a;
		if (!a) {
			cout << "That’s not an integer. Try again";
			st = false;
		}
		if (a > 99 || a < 0)
		{
			cout << "Invalid number. Try again!";
			st=false;
		}
		if (st) {
			int b1 = (b - b % 10)/10;
			int b2 = b % 10;
			int a1 = (a - a % 10)/10;
			int a2 = a % 10;
			int count = 0;
			if (a1 == b1 || a1 == b2)
				count++;
			if (a2 == b1 || a2 == b2)
				count++;
			if (a != b) {
				if (count == 1)
					cout << "Incorrect (hint: one digit matches)";
				else
					cout << "Incorrect (hint: " << count << " digits match)";
			}
		}
		i++;
	} while (a!=b);


	cout << "Congratulations! You took " << i << " guesses"<< endl;
	cout << "Your number is " << a;
	_getch();
	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