Restricting inputs to numbers?

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.

Here is the relative part of the program:

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
  #include <iostream>
#include <iomanip>

using namespace 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);		
To be honest, here where you have only 4 choices, you can take the input as a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#include <string> 

string input = ""; 

do
{
    std::cout << "Please enter you Status Code.\n"; 
    std::cin >> statusCode; 
    if(statusCode != "1" && statusCode != "2" && statusCode != "3" && statusCode != "4")
    {
    //do stuff
    }
}


If you insist on converting to int though, you can use stoi. http://www.cplusplus.com/reference/string/stoi/
Well I guess that would fix the problem with the statusCode, but the taxable income is doomed. That's alright.

Thanks much for clearly showing me what sort of options I have. I appreciate the time :D
Topic archived. No new replies allowed.