Hey guys, i've got all this coding here, it's almost complete, but i've run into a few snags to make it complete.
of course, rep for help
Here are my hiccups, then i'll post the code.
What the code does:.. Its a game, you guess numbers and win money based off of how many guesses it took you to complete it, the more guesses, the less money!
1. The game is supposed to not let them guess after their 8th guess.. that part of my code doesnt work for some reason? it seems like i can do a million guesses.
2. I must have a function thats called "calcWinnings" that takes as an argument, the number of guesses it took to determine the number. I must use a switch statement to determine the winnings. This function returns the number of winnings.
heres what someone said to help someone else doing this project, but i still dont understand it.
but i cant figure out for the life of me how to get it to work.Keep track of the number of guesses and pass that value to a function called calcWinnings(). 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.
3. i get 1 warning, which i cant figure out why.
number 2 is killing me, i didnt set it up like that and now im having a darn bad time trying to figure that out and i just cant seem to do it.no code is allowed in main() and all the different functions and stuff that are there are because thats how i HAVE to do it.. weird i know.
here is the code:
i think everything is basic for coding wise, i just need to fix that problem of making it into a switch statement.
the code runs great though, play it if you want. haha
thank you so much if you can help me.Code:#include <iostream> #include <ctime> #include <cstdlib> using namespace std; double bank; double winnings; double total; int check; char number; int guess; bool done; int guessesnum = 0; int randm; int c; 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, win .75" << endl; cout << "7 guesses, win .5" << endl; cout << "8 guesses, win .25" << endl << endl; } int genrandm() { int r; srand(time(NULL)); r = rand() % 100 + 1; return r; } int checkGuess(int guess) { if (guess > randm) { check = 1; } else if (guess < randm) { check = -1; } else { check = 0; } return (check); } int game() { cout << "Welcome to the game of guesses. It only costs a dollar to play. You can double your bet." << endl << endl; cout << "Do you want to play? (y / n)" << endl; bank = 100.00; cin >> number; if (number == 'y') { winnings = 2.00; cout << "Great!, your payout will be as follows:" << endl << endl; printPayout(); bank = bank - 1; cout << "Your bank now has " << bank << " in it" << endl; randm = genrandm(); while((bank != 0)&&(number == 'y')) { guessesnum = 0; randm = genrandm(); winnings = 2.00; while ((guessesnum != 8)) { cout << "Now guess a number between 1 and 100" << endl; cin >> guess; guessesnum++; c = checkGuess (guess); if (c == 0) { bank = bank + winnings; cout << "Correct, you win " << winnings << ", your bank is now " << bank << endl; cout << "Would you like to play again?(Y/N)"; cin >> number; break; } else if (c == -1) { cout << "Sorry that's too low try higher!" << endl; winnings = winnings - .25; } else if (c == 1) { cout << "Sorry that's too high try lower!" << endl; winnings = winnings - .25; } } } } cout << "Thanks for playing, here is your bank " << bank << endl; return 0; } int main() { game(); return 0; }


LinkBack URL
About LinkBacks
no code is allowed in main() and all the different functions and stuff that are there are because thats how i HAVE to do it.. weird i know.



Reply With Quote






Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum