RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 2424 | Replies: 2
Reply
Join Date: Mar 2006
Posts: 6
Reputation: Duke_0064 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Duke_0064 Duke_0064 is offline Offline
Newbie Poster

Help C++ Loop Question Here

  #1  
Mar 20th, 2006
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;
}
Last edited by Narue : Mar 21st, 2006 at 9:33 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: C++ Loop Question Here

  #2  
Mar 21st, 2006
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?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Mar 2006
Location: Bangalore, India
Posts: 167
Reputation: dubeyprateek is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Solution Re: C++ Loop Question Here

  #3  
Mar 21st, 2006
@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;
} 
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:44 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC