I am writing code for a guessing game program. The game is supposed to generate a random number, 3 digit number and let the user guess what it is, providing feedback for every wrong input. It is also supposed to perform 10 iterations, keeping track of the number of guesses. It then should use this record to determine the fewest guesses it took to figure out the number. I have the program set to execute 2 iterations for ease of troubleshooting. It also provides the secret number generated for reference.

I have been having trouble implementing a method of logging the number of guesses it took per random number generated. I tried a function tied to int &count inside of guessCollector( ) and a few permutations of. All I can manage is to get the code to output the number of guesses during the last iteration of the game. What would be the best way to go about this? Any ideas are greatly appreciated. I've considered using an array, but have not tried tom implement one yet. We are not allowed to use global variables.

#include <iostream>
#include <time.h>

using namespace std;

void secretNum(int &answer);
//function to generate a random number from seed

bool checkGuess(int guess, int answer);
//used to check operator's guess 

void guessResponse(int guess, int answer, int count);
//provides feed back to the user; tells whether guess is too high or low

void guessCritique(int count);
//rates user based on number of guesses

void guessCollector(int &guess, int &count, int answer);
//collects guess from user

int tracker(int count);
//score keeper


void returners();
//function to repeat the sequence of functions 10 times, and count the fewest number of guesses.


int main ()
{
	
    srand(time(0));
	
	returners();
	
	return 0;
}

void secretNum(int &answer)
{
	int num = rand()%899+100;
	answer = num;
}

bool checkGuess(int guess, int answer)
{
	return (guess == answer);
}

void guessResponse(int guess, int answer, int count)
{
	if (guess < answer)
	{
		cout << "too low\n";
	}
	
	if (guess > answer)
	{	
		cout << "too high\n";
	}
	
	if (checkGuess(guess, answer))
	{
		cout << "correct! \n";
		guessCritique(count);	
	}	
}	


void guessCritique(int count)
{
	if (count < 10)
		cout << "You must know the secret\n";
	if (count == 10)
		cout << "You got lucky\n";
	if (count > 10)
	    cout << "You should be able to do better than that\n";
	cout << endl;
}	

void guessCollector(int &guess, int &count, int answer)
{
	count = 0;
	
	int input;
	do {
	cin >> input;
	count++;
	guess = input;
		
	guessResponse(guess,answer,count);
	
	} while (!checkGuess(guess,answer));
	
		
}

int tracker(int count)
{	int placeh, tracked = 10;
	placeh = count;
	
	if (placeh < tracked) 
	{
		placeh = count;
	}
	
	return placeh;
}	

void returners()
{
	int answer, guess, count;

	int tally = 1;
	for (tally <= 2; tally <= 2; tally++)  //change to 10
	{
	secretNum(answer);
	
	cout << answer << endl;                //DELETE. used during runtime to check random number
	
	guessCollector(guess, count, answer);
		tracker(count);	
	}
		
		
}

You could save the values to a file each time. Then use a function to read those values, and determine the Count you want to display.

Do you actually have to log the guesses or simply count the number of guesses and log the guess counts? If you only have to count them (which your post seems to indicate), just use a counter variable and increment it each time the user enters a guess. Then, when the game is finished, either write the contents of the count variable to a file or push it on to a vector.

Then when you're ready to display the stats, either read and analyze the log file or analyze the vector.

Edited 6 Years Ago by Fbody: n/a

using a static array seems to be the most trouble free way to do it. getting into the fstream seems complicated. Would I really need two dimensions? I dont need the program to tell me during which random number i had the lowest number of guesses, just the lowest number of guesses.

If that's all you need, then you could probably just use a tmp variable and a static count variable. If you just want the lowest number of guess for every round (i.e., if the fewest guesses it took for any given 10 rounds was 4 guesses, you want to output that) then just pass in a tmp variable, compare it to lowest_count, if lower, replace.

may be you could use a static variable.

could you elaborate on this? This was my inital strategy...but since I need to peg my static variable in a function to the count in another, I cant seem to store a variable that isnt changed. In a perfect world I would read the first count, store it, and compare all subsequent counts to see if the are smaller.

If that's all you need, then you could probably just use a tmp variable and a static count variable. If you just want the lowest number of guess for every round (i.e., if the fewest guesses it took for any given 10 rounds was 4 guesses, you want to output that) then just pass in a tmp variable, compare it to lowest_count, if lower, replace.

Ahhhh didn't see this one.
something like this?

void tracker(int &hold)
{
	static int primed;
	int tmp;
	
	primed = hold;
	
	tmp = hold;
	
	if(tmp < primed)
	{
		primed = tmp; 
	}	

}

so static variable means that it INITIALIZES only once, but can be changed? if so then I think this will do it!

Edited 6 Years Ago by Empireryan: n/a

Yes, static means that the variable is changable, but not destroyed when the function ends (the variable goes out of scope).

It seems you were thinking of the functionality of const. A const variable's value can not be changed and is destroyed when it goes out of scope.

This question has already been answered. Start a new discussion instead.