#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double moneyinbank = 500;
double moneywagered;
int randomnumber1,randomnumber2;
srand(time(NULL));
randomnumber1 = rand() % 6 + 1;
randomnumber2 = rand() % 6 + 1;
cout << "Rules of the game\n";
cout << "You have $500 in your bank.\n";
cout << "You can only wager the amount avaliable in your bank.\n";
cout << "Roll two dice, \nif you roll doubles the amount you wagered will be added to your total amount.";
cout << "\nif not, the amount you wagered will be subtracted from your total amount.";
cout << "\nYou can accumulate as much money as you please but if you reach 0 the game is over. \n";
cout << "\n ";
cout << "You have ";
cout << moneyinbank;
cout << " dollars in your bank.\n";
cout << "How much would you like to wager?\n";
cin >> moneywagered;
if (moneywagered < 0)
{
cout << "You have entered ";
cout << moneywagered;
cout << ". Invalid amount. Run program again to start over.";
exit(0);
}
elseif (moneywagered > moneyinbank)
{
cout << "You have entered ";
cout << moneywagered;
cout << ". Invalid amount.\n";
cout << "You have ";
cout << moneyinbank;
cout << " dollars in your bank. Run program again.";
exit(0);
}
elseif (moneywagered <= moneyinbank && moneywagered >= 0 )
{
while (moneyinbank > 0)
{
cout << "You have wagered ";
cout << moneywagered;
cout << " dollars.\n";
cout << "\nNow the dice is being rolled";
cout << "....\n";
cout << "\n";
srand(time(NULL));
randomnumber1 = rand() % 6 + 1;
cout << "You rolled a ";
cout << randomnumber1;
cout << " and a ";
randomnumber2 = rand() % 6 + 1;
cout << randomnumber2;
cout << ".\n";
if (randomnumber1 == randomnumber2)
{
cout << "You rolled doubles!\n";
cout << "$";
cout << moneywagered;
cout << " is being added to your bank.\n";
moneyinbank = moneywagered + moneyinbank;
cout << "You now have $";
cout << moneyinbank;
cout << " in your bank.\n";
cout << "How much would you like to wager?\n";
cin >> moneywagered;
if (moneywagered < 0 || moneywagered > moneyinbank)
{
cout << "You have entered ";
cout << moneywagered;
cout << ". Invalid amount. Run program again to start over.";
exit(0);
}
}
elseif (randomnumber1 != randomnumber2)
{
cout << "You did not roll doubles!\n";
cout << "$";
cout << moneywagered;
cout << " is being subtracted from your bank.\n";
moneyinbank = moneyinbank - moneywagered;
cout << "You now have $";
cout << moneyinbank;
cout << " in your bank.\n";
if (moneyinbank <= 0)
cout << "No more money in bank!\n GAME OVER!";
else
{
cout << "How much would you like to wager?\n";
cin >> moneywagered;
if (moneywagered > moneyinbank || moneywagered < 0)
{
cout << "You have entered ";
cout << moneywagered;
cout << ". Invalid amount.\n";
cout << "You have ";
cout << moneyinbank;
cout << " dollars in your bank. Run program again.";
exit(0);
}
}
}
}
}
else
{
cout << "You have entered an invalid amount. Run program again to start over.";
exit(0);
}
}
to deal with this you should read the data from the user AS a string. It will go nuts if you read it as an integer.
then convert the string to integer with your favorite tool, stoi() is one of the choices.
validation is on you, stoi isnt too smart so you can check all the input string for isdigit()s or whatever validation you want to do.
Extracting data from std::cin using formatted extraction (operator>>) is not easily error proof when retrieving numeric data, it is all too easy to break it.
A bit of a read, but worth the time to learn how to cut down on problems with getting numeric data from users who have fat fingers and are trying to break your code.
@OP
The attached several snippets show a couple of ways of handling user input errors. None of it is a simple catch all or quick fix. For instance someone 'could' conceivably have a name "1234" but an integer AbcZ.9 is neither an integer nor a float/integer or any other conceivable orthodox numerical value.
The nearest catch all in some instances is to get the user to verify their input.