Hello,
I have this program here>> it's a random guessing number game. It works great the only thing I can't figure out, is how to display the best score at the end of the Loops. The outer loop is suppose to keep track of the number of games played ( number of times the loop is iterated) and the best score. So basically each time the inner loop is done, it displays a score. At the end of the program, if the user selects 'N' to stop the loop or rather end the game, the best score will be displayed or (the least number of times it took the user to guess the number) CAN ANYONE HELP PLEASE????

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int main ( )
{
	int bestscore = 0;
	int games = 0;
	int guess;
	int score = 0;
    char answer;
    srand (time (0));	

			do
			{
				games ++;

				int random = rand( );
				int num = ((random % 100) + 1);

				do
				{
					score ++;

					cout << "Guess a number between 1 and 100: ";
					cin >> guess;

					if (guess > num)
					{
						cout << "The number is lower. Try Again!!\n\n";
						continue;
					}

					if (guess < num)
					{
					cout << "The number is higher. Try Again!!\n\n";
					continue;
					}

					else if (guess = num)
					{
					cout << "You've guessed correctly!\n\n";
					break;
					}

				}while (guess != num);

				cout << "Score: "<< score << endl <<endl;
				cout << "Would you like to try again? (Y/N): ";
				cin >> answer;
				cout << endl;

         }while(answer == 'Y' || answer == 'y');

			cout << "Number of games played: " << games << endl;
			cout << "Best score: " << bestscore << endl;

return 0;
}

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

Well the idea is simple.

It's akin to finding the smallest integer within a set of elements:
How do you think you might do that kiddo?

@Duke_0064
try with this code, hope your problem is solved.

// #include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int main ( )
{
	unsigned int bestscore = 0xffffffff;
	int games = 0;
	int guess;
	int score = 0;
	char answer;
	srand (time (0)); 

	do
	{
		score = 0;
		games ++;
		int random = rand( );
		int num = ((random % 100) + 1);

		do
		{
			score ++;
			cout << "Guess a number between 1 and 100: ";
			cin >> guess;

			if (guess > num)
			{
				cout << "The number is lower. Try Again!!\n\n";
				continue;
			}

			if (guess < num)
			{
				cout << "The number is higher. Try Again!!\n\n";
				continue;
			}

			else if (guess == num)
			{
				cout << "You've guessed correctly!\n\n";
				break;
			}

		}while (guess != num);

		cout << "Score: "<< score << endl <<endl;
		cout << "Would you like to try again? (Y/N): ";
		cin >> answer;
		cout << endl;
		if(score < bestscore) {
			bestscore = score ;
		}
	}while(answer == 'Y');

	cout << "Number of games played: " << games<< endl;
	cout << "Best score: " << bestscore << endl;

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.