#include <iostream>
#include <iomanip>
#include <random>
#include <cstdlib>
#include <ctime>
usingnamespace std;
// structure of arrays to pass to functions
struct Bet
{
int toHoldType, toHoldAmount;
};
int money();
int numOfBets();
void processBetArray (Bet*, int*, int);
void spinTheWheel(Bet*, int, int, int*);
int main ()
{
int amount, bets, randomNumber;
char leaveTable;
Bet *betArray;
// get amount to enter table
amount = money ();
//Seeds random number to the generator
srand(time(NULL));
while (amount > 0)
{
// get the number of bets to be made. Everything from here down will have to be in a loop to get the needed information for each spin.
bets = numOfBets ();
// dynamically allocate array of structs
betArray = new Bet [bets];
processBetArray (betArray, &amount, bets);
//Generate random number
randomNumber = rand()%37;
// spin roulette wheel
spinTheWheel (betArray, bets, randomNumber, &amount);
// ask user if they want to leave the table each spin
cout << "Would you like to leave the table? enter Y or N ";
cin >> leaveTable;
while (leaveTable != 'y' || leaveTable != 'Y')
{
if (leaveTable == 'y' || leaveTable == 'Y')
{
cout << "Thank you for playing Roulette!\n";
break;
}
elseif (leaveTable == 'n' || leaveTable == 'N')
{
cout << "Please continue to play!\n\n";
break;
}
else
{
cout << "Please enter a valid choice.\n";
cin >> leaveTable;
cout << "\n";
}
}
if (leaveTable == 'y' || leaveTable == 'Y')
{
break;
}
// release memory back to system
delete [] betArray;
}
system("pause");
return 0;
}
int money()
{
int moneyAmount;
// Allow the user to enter the amount to enter the table with an amount of money.
cout << " Welcome to Roulette!\n\n"
<< "Please enter the dollar amount to enter the table with.\n"
<< "Note, you may leave the table after any spin and are only alowed a max of 8 bets per spin.\n\n";
cin >> moneyAmount;
// input validation on amount.
while (moneyAmount < 0 || moneyAmount > 10000)
{
if (moneyAmount < 0)
{
cout<< "\n";
cout << "You cannot enter the table with a negative amount, please re-enter your amount\n";
cin >> moneyAmount;
}
elseif (moneyAmount > 10000)
{
cout<< "\n";
cout << "You cannot enter the table with more than $10,000, please re-enter your amount\n";
cin >> moneyAmount;
}
}
cout << "\n";
return moneyAmount;
}
int numOfBets()
{
int numberOfBets;
// allow user to enter the number of bets
cout << "Please enter the number of bets you would like to place\n";
cin >> numberOfBets;
cout << "\n";
// input validation on number of bets
while (numberOfBets < 0 || numberOfBets > 8)
{
if (numberOfBets < 0)
{
cout << "You cannot place a negative number of bets, Please re-enter the number of bets\n";
cin >> numberOfBets;
cout << "\n";
}
elseif (numberOfBets > 8)
{
cout << "You cannot place more than 8 bets, please re-enter the number of bets\n";
cin >> numberOfBets;
cout << "\n";
}
}
return numberOfBets;
}
void processBetArray (Bet *betArray, int *amountOfMoney, int numberOfBets)
{
int totalAmount = 0, betCount = 0, typeOfBet, amountOnBet;
int displayMoneyLeft = *amountOfMoney;
// ask user to enter bet type.
cout << "Please enter the type of each bet you would like to make and the amount on each bet.\n"
<< "The choices are: 1.) Single Number\n"
<< " 2.) Black\n"
<< " 3.) Red\n"
<< " 4.) Odd\n"
<< " 5.) Even\n"
<< " 6.) First 12\n"
<< " 7.) Second 12\n"
<< " 8.) Third 12\n"
<< " 9.) First Half\n"
<< " 10.) Second Half\n"
<< " 11.) First Column\n"
<< " 12.) Second Column\n"
<< " 13.) Third Column\n";
// make sure they do not make bets when they do not have enough money to do so
while ( (betCount < numberOfBets) && (*amountOfMoney > 0) )
{
for (int i = 0; i < numberOfBets; i++)
{
cout << "Type of bet #" << (i + 1) << "\n";
cin >> typeOfBet;
//input validation on bet type.
while (typeOfBet < 1 || typeOfBet > 13)
{
cout << "\n";
cout << "Please choose a correct option\n";
cin >> typeOfBet;
}
// Process member "toHoldType" to hold typeOfBet
betArray[i].toHoldType = typeOfBet;
cout << "Enter the amount you want to bet ";
cin >> amountOnBet;
cout << "\n";
// input validation on on amountOnBet
while (amountOnBet < 0 || amountOnBet > 500)
{
cout << "\n";
if (amountOnBet < 0)
{
cout << "You cannot enter a negative bet, please re-enter your amount\n";
cin >> amountOnBet;
}
elseif (amountOnBet > 500)
{
cout << "You cannot enter an amount greater than 500, please re-enter your amount\n";
cin >> amountOnBet;
}
}
//total up the bet amounts.
totalAmount += amountOnBet;
//let user enter another bet if bet exceed amount of money
while (totalAmount > *amountOfMoney)
{
totalAmount -= amountOnBet;
cout << "You cannot enter a bet larger than the amount of money you have.\n"
<< "Please re-enter you bet\n";
cin >> amountOnBet;
totalAmount += amountOnBet;
// if bet equal the maximum amount of money, do not allow anymore bets
if (totalAmount == *amountOfMoney)
{
cout << "You have reached your bet limit, no more bets can be placed for this spin\n\n";
betArray[i].toHoldAmount = 0;
break;
}
}
//process member "toHoldAmount" to hold amountOnBet
betArray[i].toHoldAmount = amountOnBet;
// show user how much money is left after the bet
displayMoneyLeft -= amountOnBet;
cout << "You now have $" << displayMoneyLeft << " left\n\n";
betCount++;
}
}
}
void spinTheWheel(Bet *betArray, int numOfBets, int randomNumber, int *amountOfMoney)
{
int singleNumber, totalWinnings = 0, totalLosses = 0;
for (int i = 0; i < numOfBets; i++)
{
// switch for each different type of bet in the elements of the array of structs
switch (betArray[i].toHoldType)
{
// single number bet
case 1:
{
cout << "\n";
cout << "Please pick the number for single number bet " << (i + 1) << "\n";
cin >> singleNumber;
// input validation for numbers on the board
while (singleNumber < 0 || singleNumber > 36)
{
if (singleNumber < 0)
{
cout << "Please enter a non negative number\n";
cin >> singleNumber;
}
elseif (singleNumber > 36)
{
cout << "Please enter a number that is between 0 and 36\n";
cin >> singleNumber;
}
}
// check to see if the random number landed on the single number bet
// subtract or add winnings.
if (randomNumber == singleNumber)
{
totalWinnings += (betArray[i].toHoldAmount *= 35);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// black bet
case 2:
{
// check to see if the random number landed on the single number bet
// subtract or add winnings.
if (randomNumber == 2 || randomNumber == 4 || randomNumber == 6 || randomNumber == 8 ||
randomNumber == 10 || randomNumber == 11 || randomNumber == 13 || randomNumber == 15 ||
randomNumber == 17 || randomNumber == 20 || randomNumber == 22 || randomNumber == 24 ||
randomNumber == 26 || randomNumber == 28 || randomNumber == 29 || randomNumber == 31 ||
randomNumber == 33 || randomNumber == 35)
{
totalWinnings += (betArray[i].toHoldAmount *= 1);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// red bet
case 3:
{
// check to see if the random number landed on the single number bet
// subtract or add winnings.
if (randomNumber == 1 || randomNumber == 3 || randomNumber == 5 || randomNumber == 7 ||
randomNumber == 9 || randomNumber == 12 || randomNumber == 14 || randomNumber == 16 ||
randomNumber == 18 || randomNumber == 19 || randomNumber == 21 || randomNumber == 23 ||
randomNumber == 25 || randomNumber == 27 || randomNumber == 30 || randomNumber == 32 ||
randomNumber == 34 || randomNumber == 36)
{
totalWinnings += (betArray[i].toHoldAmount *= 1);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// odd bet
case 4:
{
if (randomNumber % 2 == 1)
{
totalWinnings += (betArray[i].toHoldAmount *= 1);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// even bet
case 5:
{
if (randomNumber % 2 == 0)
{
totalWinnings += (betArray[i].toHoldAmount *= 1);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// bet in first 12
case 6:
{
if (randomNumber >= 1 && randomNumber <= 12)
{
totalWinnings += (betArray[i].toHoldAmount *= 2);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// second 12 bet
case 7:
{
if (randomNumber >= 13 && randomNumber <= 24)
{
totalWinnings += (betArray[i].toHoldAmount *= 2);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
//third 12 bet
case 8:
{
if (randomNumber >= 25 && randomNumber <= 36)
{
totalWinnings += (betArray[i].toHoldAmount *= 2);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
//first half bet
case 9:
{
if (randomNumber >= 1 && randomNumber <= 18)
{
totalWinnings += (betArray[i].toHoldAmount *= 1);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// second half bet
case 10:
{
if (randomNumber >= 19 && randomNumber <= 36)
{
totalWinnings += (betArray[i].toHoldAmount *= 1);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// first column bet
case 11:
{
if (randomNumber == 1 || randomNumber == 4 || randomNumber == 7 || randomNumber == 10 ||
randomNumber == 13 || randomNumber == 16 || randomNumber == 19 || randomNumber == 22 ||
randomNumber == 25 || randomNumber == 28 || randomNumber == 31 || randomNumber == 34)
{
totalWinnings += (betArray[i].toHoldAmount *= 2);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// second column bet
case 12:
{
if (randomNumber == 2 || randomNumber == 5 || randomNumber == 8 || randomNumber == 11 ||
randomNumber == 14 || randomNumber == 17 || randomNumber == 20 || randomNumber == 23 ||
randomNumber == 26 || randomNumber == 29 || randomNumber == 32 || randomNumber == 35)
{
totalWinnings += (betArray[i].toHoldAmount *= 2);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
// third column bet
case 13:
{
if (randomNumber == 3 || randomNumber == 6 || randomNumber == 9 || randomNumber == 12 ||
randomNumber == 15 || randomNumber == 18 || randomNumber == 21 || randomNumber == 24 ||
randomNumber == 27 || randomNumber == 30 || randomNumber == 33 || randomNumber == 36)
{
totalWinnings += (betArray[i].toHoldAmount *= 2);
*amountOfMoney += totalWinnings;
}
else
{
totalLosses += betArray[i].toHoldAmount;
*amountOfMoney -= betArray[i].toHoldAmount;
}
// end the program if the user has no more money left.
if (*amountOfMoney <= 0)
{
cout << "Sorry! It looks like you are all out of money...\n"
<< "Thanks for playing and see you next time\n";
system("pause");
exit(0);
}
break;
}
}
*amountOfMoney += totalWinnings;
}
// display total winnings/losses
if (totalWinnings == 0)
{
cout << "Your total losses for this spin are $" << totalLosses << "\n\n";
}
cout << "Your net gain of this spin is $" << totalWinnings << "\n\n";
cout << "Total amount of money left after this spin is " << *amountOfMoney << "\n\n";
}
Have you considered reading into a string and parsing to make sure it is an integer? Also I believe it sets the error bit when trying to do that so you could do something like:
1 2 3 4 5 6
while(!(cin >> intInput))
{
//clear error bit
//ignore anything left in buffer
//display error message
}
I am not understanding from that post how I would implement that into my program. I pieced out the code in functions that I have. Sorry for posting so much, but it shouldnt be too hard to take a look at it. I have not learned templates like that in that post yet. I have just gotten to enumerated data types and finished them off.
anything past that point, I cannot understand. But must have characters and floating points thrown out of my program before saturday lol. My professor said how we do it is up to us.
I am mainly looking for a fast easy way to put it into the loops I already have for input validation on everything, something like a trailing else statement. But if the user throws garbage like that into it on first input, the program freaks out inside the loops.
could someone please help me? this project is due tomorrow. Forget about all the code, just look at the parts where I am doing input validation and explain to me how I can stop chars and floating points from being entered into my program.
I have changed the code a bit since, but nothing major, just rearranging, cosmetic surgery you could say. Input validation loops still hold where they are
I am not understanding from that post how I would implement that into my program. ...[snip]... I have not learned templates like that in that post yet.
Without knowing all of the stuff in the code, one may still be able to understand the logic behind it. These are the steps the code uses:
Get your input as a string.
Turn your string into a stream via std::istringstream.
Extract the information from the stream (via formatted extraction.)
If the extraction was successful and there is nothing left in the stream,
then the operation was successful. Otherwise, the operation was
not successful and no valid input was extracted.
Following this sequence your money function might look something like the following:
#include <iostream>
#include <sstream>
#include <string>
using std::cout;
using std::cin;
int money()
{
cout << " Welcome to Roulette!\n\n""Please enter the dollar amount to enter the table with.\n""Note, you may leave the table after any spin and are only alowed a max of 8 bets per spin.\n\n";
int moneyAmount;
bool success = false;
while (!success)
{
// Get your input as a string.
std::string token;
cin >> token;
// Turn your string into a stream viastd::istringstream.
std::istringstream stream(token);
// Extract the information from the stream(via formatted extraction.)
stream >> moneyAmount;
char dummy; // To facilitate checking if anything is left in the stream.
// If the extraction was successful and there is nothing left in the stream,
// then the operation was successful.Otherwise, the operation was
// not successful and no valid input was extracted.
if (stream && // extraction was successful.
!(stream >> dummy)) // and there is nothing left in the stream
{
// Now we know that we extracted the right type of information. Let's
// check to see if it falls into the expected range.
if (moneyAmount < 0)
cout << "\nYou cannot enter the table with a negative amount, please re-enter your amount\n";
elseif (moneyAmount > 10000)
cout << "You cannot enter the table with more than $10, 000, please re-enter your amount\n";
else
success = true;
}
else
cout << "Invalid input. Please re-enter your amount\n";
}
return moneyAmount;
}
Thank you, that is a very well defined process that I am able to really understand. Thanks a lot. I was working on trying to understand your other posts about it, but I need to go further into my degree.