| | |
Number Game
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 22
Reputation:
Solved Threads: 0
Alright I have been working on this game for like a week or 2 now, but I am having some trouble towards the end.
Heres the code I have gotten so far:
I am missing this part I just realized and dont know how I can take it out and make it a function.
I also realized it doesn't take a dollar away when you lose.
another part I am also missing is
Anyone know of a good way to add these. I have been trying and keep doing something wrong. If someone could help me out here I would be so thankful
Heres the code I have gotten so far:
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; char play; int guess; bool done; int noOfGuesses = 0; int random; int c; double bank, winnings, total; int check; void printPayout() { cout << "1 guess, win 2.00" << endl; cout << "2 guesses, win 1.75" << endl; cout << "3 guesses, win 1.5" << endl; cout << "4 guesses, win 1.25" << endl; cout << "5 guesses, win 1.00" << endl; cout << "6 guesses, .75" << endl; cout << "7 guesses, win .5" << endl; cout << "8 guesses, win .25" << endl << endl; } int checkGuess(int guess) { if (guess > random) { check = 1; } else if (guess < random) { check = -1; } else { check = 0; } return (check); } int genRandom() { int r; srand(time(NULL)); r = rand() % 100 + 1; return r; } int game() { cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl; cout << "Do you want to play (y / n)" << endl; bank = 100.00; cin >> play; if (play=='y') { winnings = 2.00; cout << "Great!, your payout will be as follows:" << endl << endl; printPayout(); random = genRandom(); while((bank!=0)&&(play=='y')) { noOfGuesses=0; random = genRandom(); winnings = 2.00; while ((noOfGuesses!=8)) { cout << "Now guess a number between 1 and 100" << endl; cin >> guess; noOfGuesses++; c = checkGuess (guess); if (c == 0) { bank = bank + winnings; cout << "Correct, you win " << winnings << ", your bank is now "<< bank<< endl<<"Do You Wanna Play Again??(Y/N)"; cin>>play; break; } else if (c == -1) { cout << "Sorry too low try higher" << endl; winnings = winnings - .25; } else if (c == 1){ cout << "Sorry too high try lower" << endl; winnings = winnings - .25; } } } } cout<<"Thank You For Playing, you are left with a total of " <<bank<<" $\n Wishing you will be back to play once more."; return 0; } int main() { game(); return 0; }
I am missing this part I just realized and dont know how I can take it out and make it a function.
•
•
•
•
4. You must have a function called calcWinnings that takes as an argument, the number of guesses it took to determine the number. You must use a switch statement to determine the winnings. This function returns the number of winnings.
I also realized it doesn't take a dollar away when you lose.
another part I am also missing is
•
•
•
•
3. You must ask the user if they want to continue after each correct guess. This means they cannot quit in the middle of a game
Last edited by FtKShadow; Oct 14th, 2008 at 4:03 pm.
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
The wording in 4) suggests that you don't need to calculate the winnings on the fly lines 90 and 94. Keep track of the number of guesses and pass that value to a function called calcWinnings() which is called on line 81 . Use the value passed in as the conditional in the switch statement. The switch statement will look a lot like the printPayout function except that it will use case statements indicating what the winnings are based on the value passed in. The return value of the function can be stored in the variable winnings and that value added to the bank.
Except for possible case sensitivity considerations it looks like you already have 3) completed.
Except for possible case sensitivity considerations it looks like you already have 3) completed.
•
•
Join Date: Aug 2008
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
The wording in 4) suggests that you don't need to calculate the winnings on the fly lines 90 and 94. Keep track of the number of guesses and pass that value to a function called calcWinnings() which is called on line 81 . Use the value passed in as the conditional in the switch statement. The switch statement will look a lot like the printPayout function except that it will use case statements indicating what the winnings are based on the value passed in. The return value of the function can be stored in the variable winnings and that value added to the bank.
Except for possible case sensitivity considerations it looks like you already have 3) completed.
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
Add another conditional to the inner while loop. The inner loop should continue if the number of guesses is less than 8 and the correct number hasn't been guessed. Use a flag to keep track of whether the correct number has been guessed, maybe a boolean variable called notguessed set to default true each new "game" and changed to false if c == 0.
Move everything except changing the notguessed flag and the break statement in the if c == 0 scenario outside of the inner while loop and place it after the inner while loop and before the restart of the outer while loop.
Determine why the inner file loop stopped. If it was because the correct answer was guessed then output congratulatory response. If it was because the correct value wasn't guessed in 8 tries, then deduct the appropriate amount from the appropriate variable.
In either case indicate the current "winning" or "bank" value or whatever and ask if they want to play another game.
Be sure to set all necessary flags and counters to the appropriate default values before each game is played.
Move everything except changing the notguessed flag and the break statement in the if c == 0 scenario outside of the inner while loop and place it after the inner while loop and before the restart of the outer while loop.
Determine why the inner file loop stopped. If it was because the correct answer was guessed then output congratulatory response. If it was because the correct value wasn't guessed in 8 tries, then deduct the appropriate amount from the appropriate variable.
In either case indicate the current "winning" or "bank" value or whatever and ask if they want to play another game.
Be sure to set all necessary flags and counters to the appropriate default values before each game is played.
Last edited by Lerner; Oct 14th, 2008 at 10:28 pm.
•
•
Join Date: Aug 2008
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
May you please PM me the entire assignment? I like these kind of game problems ^^. I would ask for you to post it but I don't think that's what you're here for. It may also help me understand the problem more in depth and help if I can.
http://www.glennstevenson.com/c++onl...8/midterm.html
![]() |
Similar Threads
- How do i create a simple game using c++?? (C++)
- Number Game- Reset, instructions, ehancements! (VB.NET)
- Guess Number Game (Problem) (Java)
- The Number Game (Software Development Job Offers)
- Help with random number gen (C++)
Other Threads in the C++ Forum
- Previous Thread: Help
- Next Thread: move bitmap image in visual C++ 2003
| Thread Tools | Search this Thread |
api array based binary 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 integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






