Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- int main() {
- double balance = 15.00;
- char choice;
- char coinchoice;
- int coinFlip;
- string flip;
- if (balance < 0) {
- cout << "Can't play, sorry" << endl;
- }
- 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"
- << endl;
- cout << "Would you like to play? <Y/N>" << endl;
- do {
- do {
- cin >> choice;
- choice == tolower(choice);
- } while ((choice != 'y' && choice != 'n') && cout << "Valid choice, please" << endl);
- if (choice == 'y' && balance >= 0.00) {
- cout << fixed << setprecision(2) << "You have $" << balance << endl;
- cout << "Guess heads or tails and I will tell you if you guessed correctly. <H/T> " << endl;
- cin >> coinchoice;
- coinchoice = tolower(coinchoice);
- 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
- {
- cout << "That is not a valid input...\nGuess either heads or tails. <H/T>" << endl;
- cin >> coinchoice;
- }
- coinchoice = coinchoice == 'h' ? 1 : 0;
- srand(time(NULL));
- coinFlip = rand() % 2; // Use the random number function to get either 0 or 1 and then an if statement to treat 0 to heads.
- flip = coinFlip == 1 ? "heads" : "tails";
- cout << "The coin landed on " << flip << endl;
- if (coinchoice == coinFlip) {
- cout << "You win!" << endl;
- balance += 1.00;
- } else {
- cout << "You lose! " << endl;
- balance -= 1.00;
- }
- cout << "Your balance is now: " << balance << endl;
- std::cout << "Play again? " << std::endl;
- }
- } while (choice != 'n' && balance >= 0);
- cout << "Thank you for playing. Your bank balance is now $" << balance << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement