| | |
random number guess
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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?
What is wrong with the guess function?
c++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
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)
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. C++ Syntax (Toggle Plain Text)
int guess; int num; int sum = 0; int numOfGuesses; if (guess == num)
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
#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; }}
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
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:
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.
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
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.
C++ Syntax (Toggle Plain Text)
/* 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.
C++ Syntax (Toggle Plain Text)
( 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.
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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?
could someone look at the statement Lines 83-85 for me?
c++ Syntax (Toggle Plain Text)
#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; }}
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
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?
C++ Syntax (Toggle Plain Text)
#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; } }
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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?)
c++ Syntax (Toggle Plain Text)
#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;
![]() |
Similar Threads
- Help with guess a number program (C++)
- Convert to Absolute Values (C)
- Guess Number Game (Problem) (Java)
- C++ Random Numbers (C++)
- RAND_MAX: how do i set? (C++)
- generate random question (C)
- Random guessing game (C++)
- Help with random number gen (C++)
Other Threads in the C++ Forum
- Previous Thread: How to know when a window of another process is activated
- Next Thread: Flow charts for currency conversion
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






