C++ Guessing Game Program

Page 1 of 1

2 Replies - 541 Views - Last Post: 27 February 2011 - 02:56 PM Rate Topic: -----

#1 mdh007  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 09-November 10

C++ Guessing Game Program

Posted 27 February 2011 - 12:35 PM

Can anyone please help me with this project im working on. I have to create a program for C++ that is a guessing game here is the code i have and the way im suppose to do it. Its not running correctly can somebody help me please ???

Thanks

 
//TODO Add comments for the main file
//TODO Add comments for each function that does not have them.
/**
 * @file Project1.cpp
 * @brief Guessing Game - This program is a game to guess what number
                was generated
 *

 * 


//TODO add any needed references to C++ header files
//	   and anything else needed to set up the program.
#include<iostream>
#include<istream>
#include<time.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;


//TODO add the definitions for the constants in the program



    int MAX_GUESSES;
    int MAX_GUESS;
    int MIN_GUESS;

//End definiton of constants



//declarations for all of the functions in the program
void guessingGame(istream &input, ostream &output);
void playGame(istream &input, ostream &output);
int generateValueToGuess();
void printPrompt(ostream &output);
int readValue(istream &input);
bool processGuess(int value, int guess, ostream &output);
void printMessage(int difference, ostream &output);
void printStatus(int guessesRemaining, ostream &output);
void printError(int badGuess, ostream &output);
void printWin(int guessesRemaining, ostream &output);
void printLoss(int value, ostream &output);


int main()
{
	//start the random number generator
	srand(time(NULL) );
	//play game
	guessingGame(cin, cout);

	return 0;
}



void guessingGame(istream &input, ostream &output)
{
	//TODO declare variables here.
	//	   play should be initialized to true

bool play = true;
char inputChar;

	while (play) //continue while they want to play
	{
		playGame(input, output); //play a game


		//TODO add a prompt that asks the user if another game is desired.
		//	   (Pressing y will start another game)
		//     Use the variable output to write the data instead of cout.
		//     Do this for all of your output statements.

output << "Do you want to play another game? Press ""Y"" if you do\n";

		input >> inputChar; //get an answer
		//check if they said yes
		if (inputChar != 'y' && inputChar != 'Y')
		{
			play = false;
		}
	}

	//TODO add a message indicating the game is over.

output << "Sorry Game Is Over";
}

void playGame(istream &input, ostream &output)
{
	//TODO declare variables here.
	//	   i should be initialized to 0
	//	   won should be initialized to false

    char i = 0;
    bool won = false;
    int  hiddenValue;

    int guess;




	//generate the value to guess
	hiddenValue = generateValueToGuess();

	//cheat to help debugging
	//output << "The number to guess is "  << hiddenValue << "\n";


	//keep going until they win or used up 10 guesses
	while ((i < MAX_GUESSES) && (!won))
	{
		//print status, prompt, get a guess
		printStatus(MAX_GUESSES - i, output);
		printPrompt(output);
		guess = readValue(input);

		//check to see if it is in bounds
		if (guess >= MIN_GUESS && guess <= MAX_GUESS)
		{
			won = processGuess(hiddenValue, guess, output);
		}
		else
		{	//out of bounds
			printError(guess, output);
		}

		i++; //another guess used up
	}

	if (won) //if the person has won
	{
		printWin(MAX_GUESSES - i, output);
	}
	else //lost
	{
		printLoss(hiddenValue, output);
	}
}


/** \brief Generates a random number to guess.
 *
 *  Generates a random number between MIN_GUESS
 *  and MAX_GUESS (1 and 100)
 *
 * \return random number to guess
 *
 */
int generateValueToGuess()
{
	//TODO declare variables here.
	//     value is a whole number

int value;




	//create a random whole number between 1 and 100
	value = static_cast<int> (rand() % (MAX_GUESS - MIN_GUESS + 1) + MIN_GUESS);

	return value;
}


void printPrompt(ostream &output)
{
	//TODO print a prompt that asks the user to enter a number.
	//	   The prompt should include the minimum and maximum valid guesses.
	//	   Use the constants you defined above when printing this information.
output << "Please enter a number to guess \n";

}

int readValue(istream &input)
{
	//TODO declare variables here.
	//     numRead is a whole number

int numRead;
	//get a number
	input >> numRead;

	return numRead;
}

bool processGuess(int value, int guess, ostream &output)
{
	//TODO declare variables here.
	//     diff is a whole number
	//     hasWon should be initialized to false

int diff;
bool hasWon = false;



	//calculate the distance between value and guess
	diff = abs(value - guess);

	//display a message based on how close it is
	printMessage(diff, output);

	if (diff == 0)  //the same, so the game is won
	{
		hasWon = true;
	}


	return hasWon;
}

void printMessage(int difference, ostream &output)
{
	if (difference == 0) //same
	{
		//do nothing
	}
	else
	{
		if (difference < 10) //hot 1 - 9
		{
			//TODO print a message saying that they are getting hot.
output << "You are getting Hot";

		}
		else
		{
			if (difference < 25)  //warm 10 - 24
			{
				//TODO print a message saying that they are warm.
output << "You are getting warm";

			}
			else //cold 25+
			{
				//TODO print a message saying that they are cold.
output  << "You are cold";

			}
		}
	}
}


/** \brief Prints the number of remaining guesses.
 *
 *  Prints a brief message to the user stating the
 *  number of remaining guesses.  This should not
 *  just print a number
 *
 * \param guessesRemaining  number of guesses left
 * \param output    output stream to write to (instead
 *                  of cout)
 *
 *
 */
void printStatus(int guessesRemaining, ostream &output)
{
	//TODO print a message giving the number of remaining guesses.
	//     The variable guessesRemaining holds this information.

output << "You have" << guessesRemaining << " left";
}


void printError(int badGuess, ostream &output)
{
	//TODO print a message telling the user that the last guess not valid.
	//     The value of the last guess should be displayed
	//     The variable badGuess holds this information.
output << "The last guess" << badGuess << " Is not valid";

}

void printWin(int guessesRemaining, ostream &output)
{
	//TODO print a message telling the user that the game has been won.
	//     The number of remaining guesses should be displayed
	//     The variable guessesRemaining holds this information.
output << "Your game has been won with" << guessesRemaining << " guesses left ";

}
void printLoss(int value, ostream &output)
{
	//TODO print a message telling the user that the game has been lost.
	//     The number the user was trying to guess should be displayed
	//     The variable value holds this information.

output << "Im sorry you have lost the game. The correct answer was" << value;
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ Guessing Game Program

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: C++ Guessing Game Program

Posted 27 February 2011 - 02:40 PM

Quote

Its not running correctly can somebody help me please ???


Please tell us precisely what is wrong. Does the program compile without error/warnings? If not what are they exactly. Does it produce any output? If it does what are you inputting into the program? What output does it produce? What do you expect?

Edit: Please don't open a new topic when you already have a topic open for this question.


Jim

This post has been edited by jimblumberg: 27 February 2011 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: C++ Guessing Game Program

Posted 27 February 2011 - 02:56 PM

Jim, that's sort of my bad I think...I believe I moved this from the C++ Programmer's forum, didn't notice it already existed.

Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1