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