954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with guess a number program

i have the program written but i am having troble with a few parts. i am supposed to have the number of tries on the screen (which i do) but i need the program to ask the player if they want to play again after the game is done and act accordingly. it also should display the number of games played, number of correct guesses and average number of guesses it took to get the right answer. i do not know where the codes should go or how to write them in. any help would greatly be appreciated. i have been trying to learn this stuff from the book examples. i hope i coded this the right way

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()
{
	int num;
	int guess;
	bool done;
	int noOfGuesses = 0;
	int ncount;
	num = (rand() + time(0)) % 1000;
	done = false;
	while ((noOfGuesses < 10) && (!done))
	
	{
		cout << "Enter an integer greater"
			<< " than or equal to 0 and "
			<< "less than 1000: ";
		cin >> guess;
		cout << endl;
		noOfGuesses++;
		if (guess == num)
		{
			cout << "you guessed the correct "
				<< "number." << endl;
			done = true;
		}
		else
			if (guess < num)
				cout << "Your guess is lower "
				<< "than the number. \n"
				<< "Guess again!" << endl;
			else
				cout << "Your guess is higher "
				<< "than the number.\n"
				<< "guess again!" << endl;
			cout <<"Total gueses equal " << noOfGuesses << endl;

		
	}
	return 0;
	}
dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Guess which step you forgot ?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

>but i need the program to ask the player if they want to play again after the game is done and act accordingly

Try creating a simple program; one that concentrates purely on getting the program to loop back to the beginning when the user chooses the option to play again.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

put the whole thing you want to repeat in a do- while loop...
ask each time before the loop ends if the user wants to play again...
and keep a count of no. of times the loop runs...that would be no. of games played...
sum up noOf guesses..like

int sum=0;//outside the loop
sum+=noOfguesses;//inside the loop
avgNoOfGuesses=sum/noofgamesplayed;//outside the loop after it terminates
tracethepath
Junior Poster in Training
54 posts since Jul 2007
Reputation Points: 8
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You