Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. double balance = 15.00;
  9. char choice;
  10. char coinchoice;
  11. int coinFlip;
  12. string flip;
  13.  
  14. if (balance < 0) {
  15. cout << "Can't play, sorry" << endl;
  16. }
  17.  
  18. cout << "Welcome to the coin flip game!\nIt will cost 1 dollar to play.\nIf you guess correctly, you will match your bet one to one.\n"
  19. << endl;
  20. cout << "Would you like to play? <Y/N>" << endl;
  21.  
  22. do {
  23. do {
  24. cin >> choice;
  25. choice == tolower(choice);
  26. } while ((choice != 'y' && choice != 'n') && cout << "Valid choice, please" << endl);
  27.  
  28. if (choice == 'y' && balance >= 0.00) {
  29.  
  30. cout << fixed << setprecision(2) << "You have $" << balance << endl;
  31. cout << "Guess heads or tails and I will tell you if you guessed correctly. <H/T> " << endl;
  32. cin >> coinchoice;
  33. coinchoice = tolower(coinchoice);
  34. while (coinchoice != 'h' && coinchoice !=
  35. 't') // this has to be right after the h/t input so the console doesn't give you a coinflip value
  36. {
  37. cout << "That is not a valid input...\nGuess either heads or tails. <H/T>" << endl;
  38. cin >> coinchoice;
  39. }
  40.  
  41. coinchoice = coinchoice == 'h' ? 1 : 0;
  42. srand(time(NULL));
  43. coinFlip = rand() % 2; // Use the random number function to get either 0 or 1 and then an if statement to treat 0 to heads.
  44. flip = coinFlip == 1 ? "heads" : "tails";
  45.  
  46. cout << "The coin landed on " << flip << endl;
  47. if (coinchoice == coinFlip) {
  48. cout << "You win!" << endl;
  49. balance += 1.00;
  50. } else {
  51. cout << "You lose! " << endl;
  52. balance -= 1.00;
  53. }
  54.  
  55. cout << "Your balance is now: " << balance << endl;
  56. std::cout << "Play again? " << std::endl;
  57. }
  58.  
  59. } while (choice != 'n' && balance >= 0);
  60.  
  61. cout << "Thank you for playing. Your bank balance is now $" << balance << endl;
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement