### Number Game

Alright I have been working on this game for like a week or 2 now, but I am having some trouble towards the end. I need it for c++ work, but I have gotten so far I am sure someone can give me a little help with some bits.

Heres the code I have gotten so far:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110`` ``````#include #include #include 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 " <

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

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 :)
4.
 ``1234567891011121314`` ``````double calcWinnings(int numberOfGuesses) { double ret = 0.0; switch(numberOfGuesses) { case 1: ret = 2.00; break; case 2: ret = 1.75; break; } return ret; }``````

3.
 ``12345678910111213141516171819`` ``````int main() { while (game()); // Continue while game returns true; } bool game() { // Do Game Stuff // Something like will suffice: string answer = ""; cout << "play again? "; getline(cin, answer); if ((answer == "Y") || (answer == "y")) return true; return false; }``````

Not use the || ! It will accept any letter!

Use this:

`if (!(variable != 'y' && variable != 'Y'))`

thanks to Jsmith.
 Not use the || ! It will accept any letter!

My code will only accept Y or y, all other values are to be considered a negative response. If the answer is Y or y then it'll return true, thus continuing the while-loop and re-calling the game function. Do notice I am doing a string comparison, not a char comparison, and I am using getLine() not >>.

Happy to be corrected, but you should test other peoples code before you say it won't work.

Code:
 ``1234567891011121314151617181920212223242526272829`` ``````#include #include using std::string; using std::cout; using std::endl; using std::cin; int main() { string answer = ""; bool bCont = false; do { cout << "Loop again? " << endl; std::getline(cin, answer); if ((answer == "Y") || (answer == "y")) bCont = true; else bCont = false; } while (bCont); cout << "Finished" << endl; return 0; }``````

 Loop again? y Answer was: y Loop again? y Answer was: y Loop again? Y Answer was: Y Loop again? c Answer was: c Finished

Last edited on
Uh... :P Sorry then ^^'
HeatMan, I appreciate you citing references, though I answered your question about DeMorgan's rule without concern of the context of the question.

Although they are identical, I would never condone using

`if( !( !expr && !expr ) )`

in place of

`if( expr || expr )`

as I find the former unilaterally more confusing than the latter. More than likely you were experiencing operator precedence problems that was leading you to conclude that the former was necessary, whereas in fact it isn't.

Topic archived. No new replies allowed.