You are to write a program that plays the game of “Guess the Number”. Your program is to randomly choose a three-digit number, allow the user to guess, provide feedback indicating if the users’ guess was too high or too low, and count the number of guesses the player makes.

Your program should provide and make use of functions:

void secretNum (int& num)
Determines the secret number to be used, passing back by reference parameter.

bool checkGuess(int guess, int answer)
This function returns true if ‘guess’ is equal to ‘answer’, and false otherwise.

void guessResponse(int guess, int answer)
This function prints a message indicating if the ‘guess’ is lower or higher than the ‘answer’

void countResponse(int count)
If count is less than 10, this function prints “You must know the secret”.
If count is equal to 10, this function prints “You got lucky”.
If count is greater than 10, this function prints “You should be able to do better than that”.

Your main function will:
Determine the secret number (use the stdlib function rand for this)
Ask the user to guess
If the user did not guess correctly
Provide feedback for the guess
Repeat the above until the user guesses correctly
Output the guess count and feedback indicating how the user did

This should be repeated for 10 problems. Prior to exiting, the program should print the lowest and highest number of guesses that occurred.

* Constant variables should be provided to store any number, which would appear in the code so that these can be easily changed if, need be.


Below is what i have so far. I am getting an error when i tried to compile it. I don't know where I have made an error. I would really appreciate it if someone could help me point out what I am doing wrong.

// Pro2a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void secretNum(int&);
void guessResponse(int guess, int answer);
void countResponse(int count);

int main()
{
	int number;
	srand(time(0)); 
	secretNum (number);

	int tries = 0, spec;

	cout << "\tWelcome to Guess My Number\n\n";

	//Guessing Loop
	do
	{

		cout << "Enter a guess:";
		cin >> spec;
		++tries;
		guessResponse( spec, tries);
	
	} while (spec != number);
	countResponse(tries);
	cout << "\nyou got it! You got it in" << tries << "guesses!\n";
	system ("pause");
return 0;
}

void secretNumber(int &num)
{
	
	num = rand() % 1000 + 1; 
}

void guessResponse(int guess, int answer)
{
	if (guess > answer)
		cout << guess << " is too high!\n\n";

	if (guess < answer)
		cout << guess << "is to low!\n\n";
}

void countResponse(int count)
{ 
	if (count > 10)
		cout << "you should be able to do better than that";

	if (count < 10)
		cout << "you must know the secret";
	
	if (count = 10)
		cout << "you got lucky";
}

>>guessResponse( spec, tries)
That should be : guessResponse( spec, number)

>>if (count = 10)
That should be if (count == 10)

>>void secretNum(int&);
See the difference between that and this : void secretNumber(int &num)

i am still having problems even though i have corrected the mistakes. Also, how can I implement bool checkGuess(int guess, int answer)into my program?

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void secretNum(int &num);
void guessResponse(int guess, int answer);
void countResponse(int count);

int main()
{
	int number;
	srand(time(0)); 
	secretNum (number);

	int tries = 0, spec;

	cout << "\tWelcome to Guess My Number\n\n";

	//Guessing Loop
	do
	{

		cout << "Enter a guess:";
		cin >> spec;
		++tries;
		guessResponse( spec, number);
	
	} while (spec != number);
	countResponse(tries);
	cout << "\nyou got it! You got it in" << tries << "guesses!\n";
	system ("pause");
return 0;
}

void secretNumber(int &num)
{
	
	num = rand() % 1000 + 1; 
}

void guessResponse(int guess, int answer)
{
	if (guess > answer)
		cout << guess << " is too high!\n\n";

	if (guess < answer)
		cout << guess << "is to low!\n\n";
}

void countResponse(int count)
{ 
	if (count > 10)
		cout << "you should be able to do better than that";

	if (count < 10)
		cout << "you must know the secret";
	
	if (count == 10)
		cout << "you got lucky";
}

You haven't corrected it. Take another look at firstPerson's post.

You can implement your function just like you did the other three.

bool checkGuess(int ans, int guess)
{
	if ( ans != guess )
		return false;
	else
		return true;
}

You haven't corrected it. Take another look at firstPerson's post.

You can implement your function just like you did the other three.

bool checkGuess(int ans, int guess)
{
	if ( ans != guess )
		return false;
	else
		return true;
}

or something even easier :

bool isCorrectGuess(int answer, int guessNumber){
  return answer == guessNumber;
}

Edited 6 Years Ago by firstPerson: n/a

This article has been dead for over six months. Start a new discussion instead.