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;
	}

Dani AI

Generated

A few concrete points to finish the program that answer the original requests (ask to play again, count games/wins, compute average):

  • Seed the random generator once at program start; adding time into the random expression does not seed it. Prefer C++11's <random>, or call srand(time(nullptr)) once before generating numbers.
  • Wrap the whole per-game sequence in an outer loop (do/while or while) that asks "play again?" at the end. Reset the per-game guess counter at the start of each game.
  • Keep aggregates outside that loop: gamesPlayed, wins (correct guesses), and sumOfGuessesForWins. Compute average as sumOfGuessesForWins / wins with a check to avoid divide-by-zero.
  • Validate input (clear cin on bad input) and report total guesses for the just-finished game.

As advised, keep the sum outside the repeat loop and add the per-game guess count inside. As recommended, isolate the play-loop first; that simplifies adding stats later. 's hint about the missing step is the play-again loop and resetting counters.

A compact, modern example (C++11+) implementing those ideas:

#include <iostream>
#include <random>
#include <limits>
#include <iomanip>

int main() {
    std::mt19937 rng{std::random_device{}()};
    std::uniform_int_distribution<int> dist(0, 999);

    int games = 0, wins = 0, totalGuessesForWins = 0;
    char play = 'y';

    while (play == 'y' || play == 'Y') {
        ++games;
        int secret = dist(rng);
        int guesses = 0;
        bool won = false;

        while (guesses < 10 && !won) {
            std::cout << "Enter guess (0-999): ";
            int g;
            if (!(std::cin >> g)) {
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "Invalid input; try again.\n";
                continue;
            }
            ++guesses;
            if (g == secret) { std::cout << "Correct!\n"; won = true; }
            else if (g < secret) std::cout << "Too low.\n";
            else std::cout << "Too high.\n";
        }

        if (won) { ++wins; totalGuessesForWins += guesses; }
        std::cout << "Total guesses this game: " << guesses << '\n';
        std::cout << "Play again? (y/n): ";
        std::cin >> play;
    }

    std::cout << "Games played: " << games << "  Wins: " << wins;
    if (wins) {
        double avg = static_cast<double>(totalGuessesForWins) / wins;
        std::cout << "  Avg guesses/win: " << std::fixed << std::setprecision(2) << avg << '\n';
    } else std::cout << "  No wins to compute average.\n";
}

Notes: input validation prevents bad reads from breaking the loop; reset per-game counters inside the outer loop; compute averages only when wins > 0. For details on modern random facilities, see C++ <random>.

Recommended Answers

All 3 Replies

Guess which step you forgot ?

Member Avatar for Member #46692

>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.

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
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.