Input for while loop

I have to write a code that the user can only enter 's' or 'j'. Anything else will be rejected and send them back to the previous step. If the user enter 's' or 'j', there will be different calculating process for them. This is what I have at the moment, and it doesn't work properly.

How do I fix this? Also, my professor will try to crash the program by entering a negative income, or some random letters beside s, S, j, or J. :)

http://oi41.tinypic.com/fdbzlu.jpg

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
	cout << Name << ",please enter your taxable income: ";
	cin >> income;	
    while (income <= 0) 
	{
        cout << "Invalid input, try again. \n";
        cout << Name << ",please enter your taxable income: ";
	cin >> income;
    }

	cout << "What is your filing status? (Enter S for single or J for joint)";
	cin >> status;
	    cin >> status;
    while (status != 'j' && status != 's' && status != 'J' && status != 'S') 
	{
        cout << "Invalid input, try again. \n";
        cout << Name << ",please enter your filling status: ";
	cin >> status;
    }

	// Processing Section
	if (status == 'S' || status == 's')
	{
		if (income >= 0 || income <= 1710)
		{ tax == 0; }
		if (income > 1710 || income <= 20930)
		{ tax == 87 + 1710 * 0.03; }
		if (income > 20930 || income <= 28790)
		{ tax == 742 + 20930 * 0.08; }
		if (income > 28790)
		{ tax == 1449.60 + 28790 * 0.11; }
	}

	else if (status == 'J' || status == 'j')
	{
		if (income >= 0 || income <= 3420)
		{ tax == 0; }
		if (income > 3420 || income <= 47120)
		{ tax == 330 + 3420 * 0.04; }
		if (income > 47210 || income <= 57580)
		{ tax == 1905.40 + 47210 * 0.09; }
		if (income > 57580)
		{ tax == 2899.20 + 57580 * 0.11; }
	}


Also, did I get the calculation part right? Was there anything else wrong?
Last edited on
doesn't work properly.
What exact problem do you have?

Also your tax calculation is wrong. It should be: constantPayment + (income - threshhold)*percentage.
for example:
1
2
if (income > 1710 || income <= 20930)
{ tax == 87 + (income - 1710) * 0.03; }


Also program requirement says that program should terminate on incorrect input, not ask user again.
If I use the while statement, it loops forever. If I use the if statement, it gives me an error and crashes. If I have to terminate the program, what should I do instead?

Oh, and thanks for the correction on my calculation. :)
If I use the while statement, it loops forever. If I use the if statement
I see for places with while or if statements. which one is giving your trouble?

If you want to prematurely ennd the program you can use return 0 from main.
This is where I am having troubles with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 while (income <= 0) 
	{
        cout << "Invalid input, try again. \n";
        cout << Name << ",please enter your taxable income: ";
	cin >> income;
    }

	cout << "What is your filing status? (Enter S for single or J for joint)";
	cin >> status;
	    cin >> status;
    while (status != 'j' && status != 's' && status != 'J' && status != 'S') 
	{
        cout << "Invalid input, try again. \n";
        cout << Name << ",please enter your filling status: ";
	cin >> status;
    }


The user must enter an income equal to or greater than 0, and status input could only be s, S, j, J.
Which one is looping on you?
Actually show how income and status were declared.
The second is looping forever, regardless of what is put in. I am also concerned with the first one because I am not sure if I did it correctly or not.

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

using namespace std;

int main()
{
	// Declare Variable 
	string	Name;
	float	income, tax, status, salary;

	// Input Section
	cout << "Enter your name: ";
	getline(cin,Name);

	cout << Name << ",please enter your taxable income: ";
	cin >> income;	
    while (income < 0) 
	{
        cout << "Invalid input, try again. \n\n";
        cout << Name << ",please enter your taxable income: ";
	cin >> income;
    }
1
2
float	status;
cin >> status;

What do you think happens, if we will try to enter 's' to a float?
It's going to give me an error.

I changed my code a bit, but the problem still persists, and the calculation is completely wrong. :O

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Declare Variable 
	string	Name;
	float	income, tax, salary;
	char	status;

	// Input Section
	cout << "Enter your name: ";
	getline(cin,Name);

	cout << "\n" << Name << ",please enter your taxable income: ";
	cin >> income;	
    if (income < 0) 
	{
        cout << "Invalid input, try again. \n\n";
        exit(EXIT_FAILURE);
    }

	cout << "\nWhat is your filing status? (Enter S for single or J for joint)";
	cin >> status;

	
	// Processing Section
	if (toupper(status) == 'S')
	{
		if (income >= 0 || income <= 1710)
		{ tax = 0; }
		else if (income > 1710 || income <= 20930)
		{ tax = 87 + (income - 1710) * 0.03; }
		else if (income > 20930 || income <= 28790)
		{ tax = 742 + (income - 20930) * 0.08; }
		else if (income > 28790)
		{ tax = 1449.60 + (income - 28790) * 0.11; }
	}

	else if (toupper(status) == 'J')
	{
		if (income >= 0 || income <= 3420)
		{ tax = 0; }
		else if (income > 3420 || income <= 47120)
		{ tax = 330 + (income - 3420) * 0.04; }
		else if (income > 47210 || income <= 57580)
		{ tax = 1905.40 + (income - 47210) * 0.09; }
		else if (income > 57580)
		{ tax = 2899.20 + (income - 57580) * 0.11; }
	}
	else { cout << "invalid"; }
		salary = income - tax;
	
	// Output Section
	cout << "\n\n";

	cout << right << "Name";
	cout << setw(17) << "Status";
	cout << setw(17) << "Gross Salary";
	cout << setw(17) << "Taxes";
	cout << setw(17) << "Net Salary";
	cout << "\n\n";

	cout << right << Name;
	
	if (toupper(status) == 'S') 
	{ cout << setw(17) << "Single"; }
	else if (toupper(status) == 'J') 
	{ cout << setw(17) << "Joint"; }

	cout << setw(17) << income;
	cout << setw(17) << tax;
	cout << setw(17) << salary;

	cout << "\n\n";
	return 0;
}


If I enter s or j, the program works but the calculation is wrong. If I enter any other characters, the program crashes with a message "Tax is undefined."
You not interrupting the program if user will enter something aside from s or j.

Also you have wrong conditions: if (income >= 0 || income <= 1710) it is read as "if income larger than zero or less than one thousand seven hundred ten". Is 1000000 larger than zero? Yes, so this branch will be used instead of correct one. You want to change or statement to and one.
I'm not interrupting if the user doesn't enter s or j? So what should I do? Would it be better if I have used the switch statement instead?

I fixed the error you mentioned, thanks for pointing that out.
You do interrupt your programm in user enters negative income, do the same for staus input.
Or do the switch and give an erron in default clause.
Oh, I get what you are saying now. Now that I am 90% done, I just have little more trouble with the setw part at the end, and writing an ID block. I have to write out what the given constants are and how I did the calculation.

What is the best way to describe the calculation process? Are constants the numbers that he gave me to calculate taxes?
Topic archived. No new replies allowed.