I have a program for clas that requires me to write a random number guessing game. I completed the game but i have to use functions in the program and I am not to familiar with this. Can some one help me? Here is the code and here are the functions that I have to use

``````#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()
{
int num;
int guess;
bool done;
int noOfGuesses=0;
int ncount=0;
int sum=0;
int noofgamesplayed=0;
int avgNoOfGuesses=0

;sum+=noOfGuesses;
avgNoOfGuesses=sum/noofgamesplayed;

num = (rand() + time(0)) % 1000;
done = false;
while ((noOfGuesses < 10) && (!done))

{
cout << "Enter an integer greater"
<< " than or equal to 0 and "
<< "less than 1000: ";
cin >> guess;
cout << endl;
noOfGuesses++;
if (guess == num)
{
cout << "you guessed the correct "
<< "number." << endl;
done = true;
}
else
if (guess < num)
cout << "Your guess is lower "
<< "than the number. \n"
<< "Guess again!" << endl;
else
cout << "Your guess is higher "
<< "than the number.\n"
<< "guess again!" << endl;
cout <<"Total gueses equal " << noOfGuesses << endl;

}
return 0;
}``````

These are the functions

``````/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet);
/*
GetGuess reads in a guess. The user is not prompted for the guess in
this function. The user only gets one chance to input a guess value.
Return Value: the value of the guess if the input is valid
0 if the input guess was not valid
*/
int GetGuess(void);
/*
CalcNewMoney determines the amount of money the player has won or
lost during the last game.
Parameters:
money: the amount of money the player had going into the game
bet: the amount the player bet on the current game
guesses: the number of guesses it took the player to win.
-1 if the player did not guess correctly
Return Value: the new amount of money the player has
*/
int CalcNewMoney(int money, int bet, int guesses);
/*
PlayAgain prompts the user to play the game again and reads in a response,
using a single character to represent a yes or no reply.
Error checking is performed on that response.
Return Value: 1 if the user wants to play again
0 if the user does not want to play again.
*/
int PlayAgain(void);
/*
PlayGame plays a single game, performing all the necessary calculations,
input, and output.
Parameters:
money: the amount of money the player has at the start of the game.
Return Value: how much the player has after the game.
*/
int PlayGame(int money);
/*
Generates a random number between 1 and MAX_RANDOM_NUM, inclusive.
Return Value: the generated number
*/
int GenerateRandomNumber(void)
{ /* GenerateRandomNumber */
/* calculate and return a number in the required range */
return(rand() * MAX_RANDOM_NUM / RAND_MAX + 1);
} /* GenerateRandomNumber */
srand((unsigned)time(NULL));``````

All 18 Replies

Have you compiled and run this?

This:

``avgNoOfGuesses=sum/noofgamesplayed;``

seems like it would generate an error, since noofgamesplayed is 0, and you're dividing by it. Besides, that statement and the one above it are unnecessary.

Do you need to write the functions, or do you simply need to call them? Either way, the functions seems to have a betting system that your current game doesn't implement.

i just compiled it and I found the error. Here is the code that I got to work:

``````#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

#include <iomanip>

#include <ctime>

using namespace std;

int main ()

{

int num;

int guess;

bool done;

int numOfGuesses;

int ncount=0;

int sum=0;

int numofgamesplayed=0;

int avgnumOfGuesses=0;

char choice ;

char n;

char y;

cout << "=============================================" << endl;

cout << " Welcome to the High Low betting Game. " << endl;

cout << " You have a \$1000 to begin game. " << endl;

cout << " Valid guesses are number between 1 and 100. " << endl;

cout << "=============================================" << endl << endl;

do

{

num = (rand() + time(0)) % 100;

done = false;

++numofgamesplayed;

numOfGuesses = 1;

while ((numOfGuesses < 7) && (!done))

{

cout << " Guess " << numOfGuesses << " : ";

cin >> guess;

cout << endl;

numOfGuesses++;

if (guess == num)

{

cout << " You guessed the correct number. " << endl << endl;

done = true;

}

else

if (guess < num)

cout << " Your guess is lower than the number. " << endl << endl;

else

cout << " Your guess is higher than the number. " << endl << endl;

}

sum+= numOfGuesses ;

cout << " Sorry... the correct answer was " << num << endl;

cout<<"\n Number of Games played "<<numofgamesplayed;

cout<<"\n Total number of guesses :"<< sum;

cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;

}

while( choice != y);

cout<< endl << " Like to try the game again <y or n>? ";

cin>> choice;

return 0;

}``````

I know i wasn't able to get the beting system to work.

I'll start you off with the first function, PrintHeading(int). This is simply taking lines 44 through 52 above, taking them out of main, and putting them into PrintHeading(int). In addition, the \$1000 is no longer hard-coded in the cout statement, but instead now uses the parameter passed to the function. Lines 44 through 52 are replaced with the function call, and a function declaration is added before main. This is the easiest of your functions, but hopefully it'll help. Give the others a try. Also, not sure if you noticed, but your srand line was accidentally cut off from your program when you were splitting the program in two in your first post. Not sure where it was originally, but I found it at the bottom of the functions code in your first post and put it at the top of main.

``````#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

#include <iomanip>

#include <ctime>

using namespace std;

int main ()

{

srand((unsigned)time(NULL));

int num;

int guess;

bool done;

int numOfGuesses;

int ncount=0;

int sum=0;

int numofgamesplayed=0;

int avgnumOfGuesses=0;

char choice ;

char n;

char y;

do

{

num = (rand() + time(0)) % 100;

done = false;

++numofgamesplayed;

numOfGuesses = 1;

while ((numOfGuesses < 7) && (!done))

{

cout << " Guess " << numOfGuesses << " : ";

cin >> guess;

cout << endl;

numOfGuesses++;

if (guess == num)

{

cout << " You guessed the correct number. " << endl << endl;

done = true;

}

else

if (guess < num)

cout << " Your guess is lower than the number. " << endl << endl;

else

cout << " Your guess is higher than the number. " << endl << endl;

}

sum+= numOfGuesses ;

cout << " Sorry... the correct answer was " << num << endl;

cout<<"\n Number of Games played "<<numofgamesplayed;

cout<<"\n Total number of guesses :"<< sum;

cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;

}

while( choice != y);

cout<< endl << " Like to try the game again <y or n>? ";

cin>> choice;

return 0;

}

/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/
{
cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;
}``````

Ok, this is what I wrote for my next function but I am getting an error saying that bet is not declared. Can someone tell me what I am doing wrong?

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
/* ----------------------------------------------------------------- */
/*						Function Prototypes                          */
/* ----------------------------------------------------------------- */
void GetBet(int money, int& bet);
/* ----------------------------------------------------------------- */
int main ()

{

srand((unsigned)time(NULL));
int money = 1000;
int num;

int guess;
int numOfGuesses;
int ncount=0;
int sum=0;
int numofgamesplayed=0;
int avgnumOfGuesses=0;
bool done;
const int numMax = 7;
const int numMin = 0;
const int guessMax = 100;
const int guessMin = 1;
char choice ;
//char n;
char y;
GetBet( money, bet);
do
{
num = (rand() + time(0)) % 100;
done = false;
++numofgamesplayed;
numOfGuesses = 1;

while ((numOfGuesses < 7) && (!done))
{
cout << " Guess " << numOfGuesses << " : ";
cin >> guess;
cout << endl;
numOfGuesses++;

if (guess == num)
{
cout << " You guessed the correct number. " << endl << endl;
done = true;
}
else
if (guess < num)
cout << " Your guess is lower than the number. " << endl << endl;
else
cout << " Your guess is higher than the number. " << endl << endl;
}
sum+= numOfGuesses ;
cout << " Sorry... the correct answer was " << num << endl;
cout<<"\n Number of Games played "<<numofgamesplayed;
cout<<"\n Total number of guesses :"<< sum;
cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;
}
while( choice != y);
cout<< endl << " Like to try the game again <y or n>? ";
cin>> choice;

return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/

{

cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;

}
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet);
{
int bet;
while (( bet <= money ) && ( bet > 0))
cout << " Enter bet : " << bet;
cin >> bet;
}``````

Here are your variable declarations in main.

``````int money = 1000;
int num;

int guess;
int numOfGuesses;
int ncount=0;
int sum=0;
int numofgamesplayed=0;
int avgnumOfGuesses=0;
bool done;
const int numMax = 7;
const int numMin = 0;
const int guessMax = 100;
const int guessMin = 1;
char choice ;
//char n;
char y;``````

bet is not among them, so when the compiler gets to this line, it doesn't know what bet is.

``GetBet( money, bet);``

Note that this line at the top doesn't allow you to use bet in main:

``void GetBet(int money, int& bet);``

That's a function declaration that lets you use the GetBet function, not the variable bet.

Nor does the fact that you declare bet later in the GetBet function itself allow you to use bet in main. You need to add this line:

``int bet;``

inside main before you use this line:

``GetBet( money, bet);``

I was told that you are suppose to declare your variables in your functions

I was told that you are suppose to declare your variables in your functions

That's a very broad statement. main IS a function so if you are going to use a variable in main and you don't want to declare it globally, which you usually don't, and you haven't done so here, you need to declare it in main.

also itried what you said and still got an error. Imust be missing a step.

also itried what you said and still got an error. Imust be missing a step.

I said you had an error. I didn't say that was the only error. You need to state what the error is rather than just say you have an error.

At minimum you have two errors in this function:

``````void GetBet(int money, int& bet);
{
int bet;
while (( bet <= money ) && ( bet > 0))
cout << " Enter bet : " << bet;
cin >> bet;
}``````

One, the function declaration has a semicolon at the end of it, but not the function itself. Delete the semicolon in red. Two, You've already declared bet in the function parameters, so don't do it in the function itself. Delete `int bet;` here.

I got the enter void GetBet(int money, int& bet) function to work. it turns out that i called the int bet twice and then instead of using the while i used the if statement and it ran. Thanks

now I'm getting a bunch of error messages. now the program is saying that all of my varables are undefined and that my GetGuess function definition is illegal.
What is wrong with the guess function?

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
/* ----------------------------------------------------------------- */
/*						Function Prototypes                          */
/* ----------------------------------------------------------------- */
void GetBet(int money, int& bet);
int GetGuess(void);
/* ----------------------------------------------------------------- */
int main ()

{

srand((unsigned)time(NULL));
int money = 1000;

int bet;

int ncount=0;

int numofgamesplayed=0;
int avgnumOfGuesses=0;
bool done = true;
const int numMax = 7;
const int numMin = 0;
const int guessMax = 100;
const int guessMin = 1;
int choice =2 ;
//char n;
//char y;
GetBet( money, bet);
GetGuess();
do
{
num = (rand() + time(0)) % 100;
done == false;
++numofgamesplayed;
numOfGuesses = 1;

while( choice != 2);
cout<< endl << " Like to try the game again <y or n>? ";
cin>> choice;

return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/

{

cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;

}
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet)
{
//int bet;
if(( bet < money ) || ( bet > 0))
{
cout << " Enter bet : ";
cin >> bet;
}}
/*
GetGuess reads in a guess. The user is not prompted for the guess in
this function. The user only gets one chance to input a guess value.
Return Value: the value of the guess if the input is valid
0 if the input guess was not valid
*/
int GetGuess(void);
{
int guess;
int num;
int sum = 0;
int numOfGuesses;

if (guess == num)
{
cout << " You guessed the correct number. " << endl << endl;
done = true;
}
else
if (guess < num)
cout << " Your guess is lower than the number. " << endl << endl;
else
cout << " Your guess is higher than the number. " << endl << endl;
}
sum+= numOfGuesses ;
cout << " Sorry... the correct answer was " << num << endl;
cout<<"\n Number of Games played "<<numofgamesplayed;
cout<<"\n Total number of guesses :"<< sum;
cout<<"\n Guessing Average = :"<<sum/numofgamesplayed;
}``````
``````do
{
num = (rand() + time(0)) % 100;
done == false;
++numofgamesplayed;
numOfGuesses = 1;

while( choice != 2);
cout<< endl << " Like to try the game again <y or n>? ";
cin>> choice;

return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/

You have a brackets problem and a do loop that starts but never ends and thus `return 0;` and `void PrintHeading(int money)` are inside that do-loop that has a while that supposedly ends it, but in fact does not due to the lack of a bracket. If you format your code so that the brackets line up with each other and the do lines up with the while from an indentation standpoint, you will spot this much sooner. You need to take a step back and look at your whole program. You are coding in too many of the details of the program and the functions without looking at the overall program structure and what should go where and what function does what. Every opening bracket has a closing bracket. Every "do" needs a "while" at the end. Carefully look at each of the function specifications and read the words that describe what the function is supposed to do, what it doesn't do, what it returns, and the parameters it is passed. Also, every variable that you compare to another variable must be initialized to some value that makes sense before that comparison is made. I'm speaking in particular of this line 6 below. num and guess have completely random values. What does num represent? What does guess represent? How are they supposed to get their values? Take a step back and make sure you have a firm understanding of what exactly you want the program and the functions to do. You can't successfully code it until you know exactly what the goal is.

``````int guess;
int num;
int sum = 0;
int numOfGuesses;

if (guess == num)``````

ok I had to take a break from the functins before I went crazy. I got the program to run with a bet but when it trys to walk passed the the bet I get an error. can someone help with my beting statement.

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
/* ----------------------------------------------------------------- */
/*						Function Prototypes                          */
/* ----------------------------------------------------------------- */
void GetBet(int money, int& bet);
/* ----------------------------------------------------------------- */
int main ()

{

srand((unsigned)time(NULL));// generates a random number
int money = 1000;// beginning value for the money the user has
int num;// the number that the computer generates
int bet;// the amount of money that the player wagers
int guess;// the player guess
int numOfGuesses;// number of times the player guesses
int ncount=0;
int sum=0;
int numofgamesplayed=0;
int avgnumOfGuesses=0;
bool done;
const int numMax = 7;
const int numMin = 0;
const int guessMax = 100;
const int guessMin = 1;
int choice =2 ;
//char n;
//char y;
GetBet( money, bet);
do
{
num = (rand() + time(0)) % 100;
done = false;
++numofgamesplayed;
numOfGuesses = 1;
if((bet > 0)||( bet <= money))
{
if (guess == num)
money = bet / guess;
else
}
while ((numOfGuesses < 7) && (!done))
{
cout << " Guess " << numOfGuesses << " : ";
cin >> guess;
cout << endl;
numOfGuesses++;

if (guess == num)
{
cout << " You guessed the correct number. " << endl << endl;
done = true;
}
else
if (guess < num)
cout << " Your guess is lower than the number. " << endl << endl;
else
cout << " Your guess is higher than the number. " << endl << endl;
}
sum+= numOfGuesses ;
avgnumOfGuesses = sum/numofgamesplayed;
cout << " Sorry... the correct answer was " << num << endl;
cout << "\n Number of Games played "<<numofgamesplayed << endl;
cout << " You have \$ " << addedBalance << " dollars left " << endl;
cout << "\n Total number of guesses :"<< sum;
cout << "\n Guessing Average = :"<<avgnumOfGuesses;

}
while( choice != 2);
cout<< endl << " Like to try the game again <y or n>? ";
cin>> choice;

return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/

{

cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;

}
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet)
{
//int bet;
if(( bet < money ) || ( bet > 0))
{
cout << " Enter bet : ";
cin >> bet;
}}``````

I don't know whether this is the error you are referring to or not, but it's an error that needs to be fixed:

``````/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet)
{
//int bet;
if(( bet < money ) || ( bet > 0))
{
cout << " Enter bet : ";
cin >> bet;
}}``````

One, notice that the specification requires that the function does not return until a valid bet is entered. That means that there has to be some sort of a loop here. Currently you do not have one. You are on the right track with your condition, but not quite there.

``( bet < money ) || ( bet > 0)``

A valid bet requires that bet is less than money (you may want to change < to <=) AND that bet > 0 (if bets of 0 are allowed, change > to >=). The key word here is AND. Currently you `||` in your condition, which means "OR", not "AND".

You are also doing a comparison before there is any value assigned to bet. You don't want to do that. You need to have the user enter a bet before comparing bet to anything. So you need a loop of some type in this function, where the loop is exited when he/she enters a valid bet, and you need to make sure not to check for a valid bet until the user actually enters a bet. I would also display an error message if the user enters an invalid bet so that the he/she knows what he/she did wrong.

thax, now back to the hard stuff

i'm assuming with the additionof some code I some how killed my play again statement
could someone look at the statement Lines 83-85 for me?

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
/* ----------------------------------------------------------------- */
/*						Function Prototypes                          */
/* ----------------------------------------------------------------- */
void GetBet(int money, int& bet);	// gets bet
/* ----------------------------------------------------------------- */
int main ()

{

srand((unsigned)time(NULL));	// generates a random number
int money = 1000;				// beginning value for the money
int num;						// the number that the computer generates
int bet;						// the amount of money that the player bets
int guess;						// the player guess
int numOfGuesses;				// number of times the player guesses
int sum=0;						// total number of guesses
int numofgamesplayed=0;			// total number of games played
int avgnumOfGuesses=0;			// average number of games played
int addedBalance = 0;			// balance after money has been +-
bool done;
const int numMax = 7;			// maximum number of guesses
const int numMin = 0;			// minimum number of guesses
const int guessMax = 100;		// max range of random number
const int guessMin = 1;// min range of random number
int choice;

GetBet( money, bet);

do
{
num = (rand() + time(0)) % 100;
done = false;
++numofgamesplayed;
numOfGuesses = 1;

while ((numOfGuesses < 7) && (!done))
{
cout << " Guess " << numOfGuesses << " : ";
cin >> guess;
cout << endl;
numOfGuesses++;

if (guess == num)
{
cout << " You guessed the correct number. " << endl << endl;
done = true;
}
else
if (guess < num)
cout << " Your guess is lower than the number. " << endl << endl;
else
cout << " Your guess is higher than the number. " << endl << endl;
}

sum+= numOfGuesses ;
if((bet > 0)&&( bet <= money))
{
if (guess == num)
money = bet / guess;
else
if ((bet > money)|| (bet < 0))
cout << " Please enter a vaild bet " <<endl;
}
avgnumOfGuesses = sum/numofgamesplayed;
cout << " Sorry... the correct answer was " << num << endl;
cout << "\n Number of Games played "<<numofgamesplayed << endl;
cout << " You have \$ " << addedBalance << " dollars left " << endl;
cout << "\n Total number of guesses :"<< sum;
cout << "\n Guessing Average = :"<<avgnumOfGuesses;

}
while( choice != 2);
cout<< endl << " Like to try the game again <1 or 2>? ";
cin>> choice;

return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/

{

cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;

}
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet)
{
//int bet;
if(( bet < money ) || ( bet > 0))
{
cout << " Enter bet : ";
cin >> bet;
}}``````

You need to format so things line up, particularly when you are first learning, but really even as you get better. Things will pop out to you when you line things up and spaces things properly. It becomes very obvious what is inside a block of code and what is outside of that block. If you are not already using an IDE like Visual Studio Express (there are others out there too), it will simplify your life greatly to start using one. It has a formatting feature. One click of a button and your code is formatted the way you want it. Here's your code formatted and lined up. It will look far better in an IDE where the comments don't overflow onto the next line, so copy and paste it into one and it'll look better. Look at lines 89 and 90. They are not inside of any loop. Do you want them there?

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

/* ----------------------------------------------------------------- */
/*						Function Prototypes                          */
/* ----------------------------------------------------------------- */
void GetBet(int money, int& bet);	// gets bet
/* ----------------------------------------------------------------- */

int main ()
{
srand((unsigned)time(NULL));	// generates a random number
int money = 1000;				// beginning value for the money
int num;						// the number that the computer generates
int bet;						// the amount of money that the player bets
int guess;						// the player guess
int numOfGuesses;				// number of times the player guesses
int sum=0;						// total number of guesses
int numofgamesplayed=0;			// total number of games played
int avgnumOfGuesses=0;			// average number of games played
int addedBalance = 0;			// balance after money has been +-
bool done;
const int numMax = 7;			// maximum number of guesses
const int numMin = 0;			// minimum number of guesses
const int guessMax = 100;		// max range of random number
const int guessMin = 1;// min range of random number
int choice;

GetBet( money, bet);

do
{
num = (rand() + time(0)) % 100;
done = false;
++numofgamesplayed;
numOfGuesses = 1;

while ((numOfGuesses < 7) && (!done))
{
cout << " Guess " << numOfGuesses << " : ";
cin >> guess;
cout << endl;
numOfGuesses++;

if (guess == num)
{
cout << " You guessed the correct number. " << endl << endl;
done = true;
}
else if (guess < num)
cout << " Your guess is lower than the number. " << endl << endl;
else
cout << " Your guess is higher than the number. " << endl << endl;
}

sum+= numOfGuesses ;

if((bet > 0)&&( bet <= money))
{
if (guess == num)
money = bet / guess;
else

if ((bet > money)|| (bet < 0))
cout << " Please enter a vaild bet " <<endl;
}

avgnumOfGuesses = sum/numofgamesplayed;
cout << " Sorry... the correct answer was " << num << endl;
cout << "\n Number of Games played "<<numofgamesplayed << endl;
cout << " You have \$ " << addedBalance << " dollars left " << endl;
cout << "\n Total number of guesses :"<< sum;
cout << "\n Guessing Average = :"<<avgnumOfGuesses;
}
while( choice != 2);

cout<< endl << " Like to try the game again <1 or 2>? ";
cin>> choice;

return 0;
}

/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/
{
cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;
}

/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet)
{
//int bet;
if(( bet < money ) || ( bet > 0))
{
cout << " Enter bet : ";
cin >> bet;
}
}``````
commented: great help +1

I'm get an error in my function that calls the program to keep count of the money error C2447: '{' : missing function header (old-style formal list?)

``````#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
/* ----------------------------------------------------------------- */
/*						Function Prototypes                          */
/* ----------------------------------------------------------------- */
void GetBet(int money, int& bet);	// gets bet
int CalcNewMoney(int money, int bet, int numOfGuesses);
/* ----------------------------------------------------------------- */
int main ()

{

srand((unsigned)time(NULL));	// generates a random number
int money = 1000;				// beginning value for the money
int num;						// the number that the computer generates
int bet;						// the amount of money that the player bets
int guess;						// the player guess
int numOfGuesses;				// number of times the player guesses
int sum=0;						// total number of guesses
int numofgamesplayed=0;			// total number of games played
int avgnumOfGuesses=0;			// average number of games played
int addedBalance = 0;			// balance after money has been +-
bool done;
const int numMax = 7;			// maximum number of guesses
const int numMin = 0;			// minimum number of guesses
const int guessMax = 100;		// max range of random number
const int guessMin = 1;			// min range of random number
int choice =2 ;

do
{
GetBet( money, bet);
num = (rand() + time(0)) % 100;
done = false;
++numofgamesplayed;
numOfGuesses = 1;

while ((numOfGuesses < 7) && (!done))
{
cout << " Guess " << numOfGuesses << " : ";
cin >> guess;
cout << endl;
numOfGuesses++;

if (guess == num)
{
cout << " You guessed the correct number. " << endl << endl;
done = true;
}
else
if (guess < num)
cout << " Your guess is lower than the number. " << endl << endl;
else
cout << " Your guess is higher than the number. " << endl << endl;
}

sum+= numOfGuesses ;
CalcNewMoney(money, bet, numOfGuesses);
avgnumOfGuesses = sum/numofgamesplayed;
cout << " Sorry... the correct answer was " << num << endl;
cout << "\n Number of Games played "<<numofgamesplayed << endl;
cout << " You have \$ " << money << " dollars left " << endl;
cout << "\n Total number of guesses :"<< sum;
cout << "\n Guessing Average = :"<<avgnumOfGuesses;
cout<< endl << " Like to try the game again < 1 or 2>? ";
cin>> choice;
}
while( choice != 2);

return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/

{

cout << "=============================================" << endl;
cout << " Welcome to the High Low betting Game. " << endl;
cout << " You have \$" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;

}
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is entered.
Parameters:
money: the amount of money the player currently has
bet: the bet chosen by the user
*/
void GetBet(int money, int& bet)
{
if(( bet < money ) || ( bet > 0))
{
cout << " Enter bet : ";
cin >> bet;
}}
/*
CalcNewMoney determines the amount of money the player has won or
lost during the last game.
Parameters:
money: the amount of money the player had going into the game
bet: the amount the player bet on the current game
guesses: the number of guesses it took the player to win.
-1 if the player did not guess correctly
Return Value: the new amount of money the player has
*/
int CalcNewMoney(int money, int bet, int numOfGuesses);
{
if((bet > 0)&&( bet <= money))
{
if (guess == num)
money = bet / guess;
else
money = money - bet;
}}
//if ((bet > money)|| (bet < 0))
//cout << " Please enter a vaild bet " <<endl;``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.