Advertisement
oaktree

Heads or Tails

Jun 20th, 2016
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. void game() {
  6.     std::cout << "[*] How much money do you have? ";
  7.     int user_money; std::cin >> user_money;
  8.  
  9.     int user_bet, user_guess, toss;
  10.     while (user_money > 0) {
  11.         std::cout << "[*] Place a bet. ";
  12.         std::cin >> user_bet;
  13.         if (user_bet > user_money) {
  14.             std::cout << "Your bet was too large! Try again..." << std::endl;
  15.             continue;
  16.         }
  17.         while (true) {
  18.             std::cout << "Guess: 0 for heads, 1 for tails. ";
  19.             std::cin >> user_guess;
  20.  
  21.             toss = rand() % 2;
  22.  
  23.             if (user_guess >= 0 && user_guess <= 1) {
  24.                 std::cout << "You guessed " << (user_guess == 0 ? "heads" : "tails")
  25.                     << " and were ";
  26.                 if (toss == user_guess) {
  27.                     std::cout << "correct!";
  28.                     user_money += user_bet;
  29.                 } else {
  30.                     std::cout << "incorrect!";
  31.                     user_money -= user_bet;
  32.                 }
  33.                 std::cout << std::endl;
  34.                 break;
  35.             } else {
  36.                 std::cout << "Invalid guess. Try again...";
  37.             }
  38.         }
  39.  
  40.         std::cout << "You now have $" << user_money << ".\n";
  41.     }
  42.  
  43.     std::cout << "You're out of money." << std::endll;
  44. }
  45.  
  46. int main() {
  47.     std::cout << "[*] Heads or Tails [*]" << std::endl
  48.         << "You will bet on the outcome of a coin toss." << std::endl
  49.         << "You can only bet as much as you have." << std::endl;
  50.  
  51.     game();
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement