daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         return 0;
  17.     }
  18.  
  19.     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"
  20.             << endl;
  21.     cout << "Would you like to play? <Y/N>" << endl;
  22.  
  23.     do {
  24.         do {
  25.             cin >> choice;
  26.             choice = tolower(choice);
  27.         } while ((choice != 'y' && choice != 'n') && cout << "Valid choice, please" << endl);
  28.  
  29.         if (choice == 'y' && balance >= 0.00) {
  30.  
  31.             cout << fixed << setprecision(2) << "You have $" << balance << endl;
  32.             cout << "Guess heads or tails and I will tell you if you guessed correctly. <H/T> " << endl;
  33.             cin >> coinchoice;
  34.             coinchoice = tolower(coinchoice);
  35.             while (coinchoice != 'h' && coinchoice != '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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top