3 Functions not working together

As a first year student in C++ I have been given the following assignment:
The user must guess a number between 1 and 1000. This must be done in 3 functions.
The first function must ask for input from the user.
The second function must display whether the guess is too low, too high or correct.
The third function must work out how many guesses the user had to guess the number and display that (the part I really can't figure out).
Also, when I enter 1 as my first guess it first states that my guess is too high?? and then it only starts working correctly. Why is that?

My program thus far:

//Guess the secret number
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int getGuess(int valueGuessed)
{
cout << "I have a number between 1 and 1000." << endl;
cout << "Can you guess my number?" << endl;
cout << "Please type your first guess:" << endl;
cin >> valueGuessed;
return valueGuessed;
}

int isCorrect(int valueGuessed, int randomNumber)
{
//Generate the random number
srand(time(0));
randomNumber = rand( ) % 1000 + 1;

while (valueGuessed != randomNumber) //loops until user finds the number
{
if (valueGuessed > randomNumber)
cout << "Too high. Try again:" << endl;
cin >> valueGuessed;

if (valueGuessed < randomNumber)
cout << "Too low. Try again:" << endl;
cin >> valueGuessed;
}
if (valueGuessed == randomNumber)
cout << "Excellent! You guessed the secret number!" << endl;
}

int analyzeCount(int noOfGuesses)
{
noOfGuesses = 0;
noOfGuesses++;

cout << "You have made " << noOfGuesses << " guesses." << endl;

if (noOfGuesses < 10)
cout << "Either you know the secret or you got lucky!" << endl;

else if (noOfGuesses == 10)
cout << "Aha! You know the secret!" << endl;

else if (noOfGuesses > 10)
cout << "You should be able to do better!" << endl;
}

int main( )
{
int valueGuessed, randomNumber, noOfGuesses;
char prompt;

getGuess(valueGuessed);

isCorrect(valueGuessed, randomNumber);

analyzeCount(noOfGuesses);

cout << "Would you like to play again? (y/n): ";
cin >> prompt;
if (prompt == 'y')
{
getGuess(valueGuessed);
isCorrect(valueGuessed, randomNumber);
analyzeCount(noOfGuesses);
}
else
return 0;
}
Last edited on
You should practice better... You're source code is such a mess, very badly made... Try this...
Good luck

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Guess the secret number
#include <iostream>
using namespace std;

int getGuess()
{
int valueGuessed;
cout << "I have a number between 1 and 1000." << endl;
cout << "Can you guess my number?" << endl;
cout << "Please type your first guess:" << endl;
cin >> valueGuessed;
return valueGuessed;
}

bool isCorrect(int valueGuessed, int randomNumber)
{
	if (valueGuessed == randomNumber)
	{
		cout << "Excellent! You guessed the secret number!" << endl;
		return true;
	}
	else
	{
		if (valueGuessed > randomNumber)
			cout << "Too high." << endl;
		else
			cout << "Too low." << endl;
		return false;
	}
}

void howManyGuesses(int v[1000])
{
	int i=0;
	while (v[i]!=0)
		i++;
	cout << "You have made " << i << " guesses." << endl;
	if (i < 10)
		cout << "Either you know the secret or you got lucky!" << endl;
	else
		cout << "You should be able to do better!" << endl;
}


int main(){
	int a,randomNumber,v[1000],i=0;;
	char prompt;
	srand(time(0));
	do{
		for (i=0;i<1000;i++)
	   	v[i]=0;
	   i=0;
		//Generate the random number
		randomNumber = rand( ) % 1000 + 1;
		do{
			a=getGuess();
			v[i]=a;
			i++;
		} while (isCorrect(a, randomNumber)==false);
		howManyGuesses(v);
		cout << "Would you like to play again? (y/n): ";
		cin >> prompt;
	} while (prompt == 'y');
	cin.ignore();
	cin.get();
}
Topic archived. No new replies allowed.