8 Replies - 4547 Views - Last Post: 28 February 2011 - 04:01 AM Rate Topic: -----

#1 mdh007  Icon User is offline

  • New D.I.C Head

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

Guessing Game for C++

Posted 27 February 2011 - 12:15 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: Guessing Game for C++

#2 LegalBreaker  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 25-June 09

Re: Guessing Game for C++

Posted 27 February 2011 - 12:18 PM

This is the Java section...
Was This Post Helpful? 1
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Guessing Game for C++

Posted 27 February 2011 - 12:35 PM

@LegalBreaker, easier if you just report it if you didn't already. :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,539
  • Joined: 27-December 08

Re: Guessing Game for C++

Posted 27 February 2011 - 12:42 PM

If this is C++, then WHY are you posting this in Java?!

Moved to C++.
Was This Post Helpful? 0
  • +
  • -

#5 Crimson Wings  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 215
  • Joined: 11-July 09

Re: Guessing Game for C++

Posted 27 February 2011 - 12:45 PM

That's a long code. Tell us how it is supposed to work and how it isn't working. Not all of us like to compile others' code unless we find it strictly necessary.
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Guessing Game for C++

Posted 27 February 2011 - 01:08 PM

Quote

Its not running correctly


I'm sure that you are well aware of the problem with this statement, it is very very vague. Tell us what the problem is exactly and what you have done to solve it, what was the result?
Was This Post Helpful? 0
  • +
  • -

#7 mdh007  Icon User is offline

  • New D.I.C Head

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

Re: Guessing Game for C++

Posted 27 February 2011 - 03:03 PM

It compiles and runs and outputs

Im sorry you have lost the game. The correct answer was0Do you want to play another game? Press Y if you do

If i enter y or Y it keeps outputing the same thing, if i enter a letter besides y it outputs Sorry Game is Over.

Can somebody help ????
Was This Post Helpful? -1
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,866
  • Joined: 25-December 09

Re: Guessing Game for C++

Posted 27 February 2011 - 03:14 PM

You have several Global variables that are not initialized (at about line 28). Also these should be declared constants which you have not done.

const int MAX_NUM = 43;




Jim
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Guessing Game for C++

Posted 28 February 2011 - 04:01 AM

View Postmdh007, on 28 February 2011 - 08:03 AM, said:

It compiles


Not what you posted.
Perhaps you posted the wrong code?
Makes it hard to get help if you give us the wrong code to look at.

DIC.cpp:149:1: warning: "/*" within comment
DIC.cpp: In function ‘int generateValueToGuess()’:
DIC.cpp:168: error: ‘rand’ was not declared in this scope
DIC.cpp:168: error: ‘MAX_GUESS’ was not declared in this scope
DIC.cpp:168: error: ‘MIN_GUESS’ was not declared in this scope
DIC.cpp: At global scope:
DIC.cpp:174: error: variable or field ‘printPrompt’ declared void
DIC.cpp:174: error: ‘ostream’ was not declared in this scope
DIC.cpp:174: error: ‘output’ was not declared in this scope
DIC.cpp:183: error: ‘istream’ was not declared in this scope
DIC.cpp:183: error: ‘input’ was not declared in this scope
DIC.cpp:184: error: expected ‘,’ or ‘;’ before ‘{’ token
DIC.cpp:195: error: ‘ostream’ has not been declared
DIC.cpp: In function ‘bool processGuess(int, int, int&)’:
DIC.cpp:207: error: ‘abs’ was not declared in this scope
DIC.cpp:210: error: ‘printMessage’ was not declared in this scope
DIC.cpp: At global scope:
DIC.cpp:221: error: ‘ostream’ has not been declared
DIC.cpp: In function ‘void printMessage(int, int&)’:
DIC.cpp:232: error: invalid operands of types ‘int’ and ‘const char [20]’ to binary ‘operator<<’
DIC.cpp:240: error: invalid operands of types ‘int’ and ‘const char [21]’ to binary ‘operator<<’
DIC.cpp:246: error: invalid operands of types ‘int’ and ‘const char [13]’ to binary ‘operator<<’
DIC.cpp: At global scope:
DIC.cpp:266: error: ‘ostream’ has not been declared
DIC.cpp: In function ‘void printStatus(int, int&)’:
DIC.cpp:271: error: invalid operands of types ‘int’ and ‘const char [9]’ to binary ‘operator<<’
DIC.cpp: At global scope:
DIC.cpp:275: error: ‘ostream’ has not been declared
DIC.cpp: In function ‘void printError(int, int&)’:
DIC.cpp:280: error: invalid operands of types ‘int’ and ‘const char [15]’ to binary ‘operator<<’
DIC.cpp: At global scope:
DIC.cpp:284: error: ‘ostream’ has not been declared
DIC.cpp: In function ‘void printWin(int, int&)’:
DIC.cpp:289: error: invalid operands of types ‘int’ and ‘const char [28]’ to binary ‘operator<<’
DIC.cpp: At global scope:
DIC.cpp:292: error: ‘ostream’ has not been declared
DIC.cpp: In function ‘void printLoss(int, int&)’:
DIC.cpp:298: error: invalid operands of types ‘int’ and ‘const char [56]’ to binary ‘operator<<’


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1