I am trying to validate this input in these functions that I have created for my roulette program. The rest of the program is much more intricate and complicated with dynamic arrays of structures and what not. Everything works. Except for trying to validate this for the right data types.
int money()
{
int validatedMoney;
// 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";
validatedMoney = validateMoney();
cout << "\n";
return validatedMoney;
}
int validateMoney()
{
int moneyAmount;
cin >> moneyAmount;
while (!(cin >> moneyAmount) || 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;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<< "\n";
cout << "Please enter an integer\n";
cin >> moneyAmount;
}
return moneyAmount;
}
but it keeps making me input it twice when I do enter the correct range and type of number that I need. It checks it just fine. Cannot enter letters or floating point data types. But when I enter the correct numerical value, i have to twice. Can someone tell me why?