| | |
random number guess
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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
These are the functions
c++ Syntax (Toggle Plain Text)
#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; }
c++ Syntax (Toggle Plain Text)
/* PrintHeading simply prints the introductory output. Parameters: initial amount of money received */ void PrintHeading(int money) /* 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));
Have you compiled and run this?
This:
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.
This:
C++ Syntax (Toggle Plain Text)
avgNoOfGuesses=sum/noofgamesplayed;
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.
Last edited by CoolGamer48; Jul 24th, 2008 at 9:38 pm. Reason: grammar
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
i just compiled it and I found the error. Here is the code that I got to work:
I know i wasn't able to get the beting system to work.
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by student4lyfe; Jul 24th, 2008 at 9:55 pm. Reason: answer the last part of the response to my question
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> using namespace std; void PrintHeading(int money); 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; PrintHeading (1000); 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 */ 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; }
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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?
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)); 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); PrintHeading(money); 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 */ 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; while (( bet <= money ) && ( bet > 0)) cout << " Enter bet : " << bet; cin >> bet; }
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
Here are your variable declarations in main.
bet is not among them, so when the compiler gets to this line, it doesn't know what bet is.
Note that this line at the top doesn't allow you to use bet in main:
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:
inside main before you use this line:
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
int bet;
inside main before you use this line:
C++ Syntax (Toggle Plain Text)
GetBet( money, bet);
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
also itried what you said and still got an error. Imust be missing a step.
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. ![]() |
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 based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






