I've written this program that estimates federal taxes, it requires two inputs, one which specifies how the user is filing, and the second is their total taxable income. I've used "if" statements inside do/while loops to restrict the inputs to certain numbers. However, if I input a letter the program freaks out and just runs through the loop without stopping to ask for input....well forever.
How can I restrict the inputs to only being numbers? I'm only halfway through an introductory C++ college course so it can't be anything to complex or fancy.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int statusCode; //This block declares the used variables as well as initiates those that need it.
float taxableIncome;
float taxAMT;
float bracketOne = 0.0;
float bracketTwo = 0.0;
float bracketThree = 0.0;
float bracketFour = 0.0;
float bracketFive = 0.0;
float bracketSix = 0.0;
//Do/while statement for input of statusCode
do
{
cout << "Please enter your Status Code." << endl //This block gives instructions on the status code.
<< "(1) If single" << endl
<< "(2) If married and filing jointly" << endl
<< "(3) If married and filing separately" << endl
<< "(4) If the head of a household" << endl << endl
<< "Your Status Code: ";
cin >> statusCode; //Input the status code.
if (statusCode < 1 || statusCode > 4) //This if statement outputs an error if an incorrect status code is input.
cout << "You must enter a correct Status Code." << endl << endl; //Error message for incorrect status code.
}
while (statusCode < 1 || statusCode > 4); //End of do/while statement for status code.
//do/while statement for input of taxableIncome
do
{
cout << "Please enter your total taxable income: ";
cin >> taxableIncome; //Input for taxable income.
cout << endl;
if (taxableIncome < 0) //This if statement outputs an error if an icorrect taxable income is input.
cout << "Must enter a positive value for taxable income." << endl; //Error message for incorrect taxable income
}
while (taxableIncome < 0);