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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````//Guess the secret number #include 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.