Here's my source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <time.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int playagain;
float getbalance()
{
float balance;
cout << "How much money can you afford to lose?";
cin >> balance;
while (balance <= 0)
{
cout << "You cannot have an initial balance of 0 or less. Please re-enter your balance.";
cin >> balance;
}
return balance;
}
int rollthedice()
{
int die = 1+rand()%6;
return die;
}
float getbet()
{
float bet;
float balance;
cout << "How much would you like to bet?";
cin >> bet;
while (bet > balance || bet <= 0)
{
if (bet > balance)
{
cout << "You cannot bet more than you are willing to lose. Please re-enter your bet.";
cin >> bet;
}
if(bet <= 0)
{
cout << "You cannot bet 0 or less. Please re-enter your bet.";
cin >> bet;
}
return bet;
}
}
bool playgame()
{
int dietotal;
int point;
int die1, die2;
die1 = rollthedice();
die2 = rollthedice();
dietotal = die1+die2;
cout << "You rolled a " << die1 << " and a " << die2 << ".";
if (dietotal == 7 || dietotal == 11)
{
return 1;
}
else if (dietotal == 2 || dietotal == 3 || dietotal == 12)
{
return 0;
}
else
{
point = dietotal;
cout << "Your point is " << point << ".";
do
{
cout << "Here we go again!" << endl;
die1 = rollthedice();
die2 = rollthedice();
dietotal = die1+die2;
cout << "You rolled a " << die1 << " and a " << die2 << ".";
}
while (dietotal != point && dietotal != 7);
if (dietotal == point)
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
double bet;
double balance;
bool Gamewon;
float playerbet;
float playerbalance;
srand(time(NULL));
playerbalance = getbalance();
do
{
playerbet = getbet();
Gamewon = playgame();
if (Gamewon == 0)
{
playerbalance = balance - playerbet;
cout << "Sorry, you lost!" << endl << "Your balance is now " << playerbalance << "." << endl;
balance = playerbalance;
}
else
{
playerbalance = balance + playerbet;
cout << "Congratulations, you won!" << endl << "Your balance is now " << playerbalance << "." << endl;
balance = playerbalance;
}
cout << "Would you like to play again?(enter a 1 if you would and a 0 if you would not)";
cin >> playagain;
while (playagain > 1 || playagain < 0)
{
cout << "Invalid input. Please re-enter.";
cin >> playagain;
}
}
while (playagain == 1 && balance > 0);
return 0;
}
|
Here's my output
How much money can you afford to lose?1000
How much would you like to bet?1000
You rolled a 2 and a 4.Your point is 6.Here we go again!
You rolled a 4 and a 6.Here we go again!
You rolled a 5 and a 3.Here we go again!
You rolled a 4 and a 5.Here we go again!
You rolled a 2 and a 1.Here we go again!
You rolled a 2 and a 2.Here we go again!
You rolled a 6 and a 3.Here we go again!
You rolled a 3 and a 1.Here we go again!
You rolled a 6 and a 5.Here we go again!
You rolled a 2 and a 3.Here we go again!
You rolled a 4 and a 1.Here we go again!
You rolled a 6 and a 2.Here we go again!
You rolled a 5 and a 1.Congratulations, you won!
Your balance is now nan.
Would you like to play again?(enter a 1 if you would and a 0 if you would not)
inb4 op is a dumbass for not seeing a simple solution.