#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> using namespace std; /* ----------------------------------------------------------------- */ /* Function Prototypes */ /* ----------------------------------------------------------------- */ void PrintHeading(int money); 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; PrintHeading(money); 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 */ void PrintHeading(int money) { 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 */ void PrintHeading(int money)
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)
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> using namespace std; /* ----------------------------------------------------------------- */ /* Function Prototypes */ /* ----------------------------------------------------------------- */ void PrintHeading(int money); 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; int addedBalance = 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; PrintHeading(money); 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 addedBalance = money - bet; } 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 */ void PrintHeading(int money) { 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; }}
/* 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; }}
( bet < money ) || ( bet > 0)
|| in your condition, which means "OR", not "AND".
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> using namespace std; /* ----------------------------------------------------------------- */ /* Function Prototypes */ /* ----------------------------------------------------------------- */ void PrintHeading(int money); //prints header 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; PrintHeading(money); 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 addedBalance = money - bet; 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 */ void PrintHeading(int money) { 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; }}
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> using namespace std; /* ----------------------------------------------------------------- */ /* Function Prototypes */ /* ----------------------------------------------------------------- */ void PrintHeading(int money); //prints header 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; PrintHeading(money); 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 addedBalance = money - bet; 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 */ void PrintHeading(int money) { 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; } }
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> using namespace std; /* ----------------------------------------------------------------- */ /* Function Prototypes */ /* ----------------------------------------------------------------- */ void PrintHeading(int money); //prints header 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 ; PrintHeading(money); 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 */ void PrintHeading(int money) { 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;
| DaniWeb Message | |
| Cancel Changes | |