Do While loop with enum data type

Just learned about the enum data type and I'm trying to use it within one of my class exercises. I'm trying to set up a do while loop using an enum variable as the argument to exit the loop but I can't seem to get it to work.

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

using namespace std;

// Function Prototypes
void getData(int& numberOfChildren, double& grossIncome, double& pensionPercent);
double taxAmount(int numberOfChildren, double grossIncome, double pensionPercent);

// User-defined Simple Data Types
enum status{Single, Married};

// Constant Variables
const int SINGLE_EXEMPTION = 4000;
const int MARRIED_EXEMPTION = 7000;
const int PERSONAL_EXEMPTION = 1500;

int main()
{
	int numberOfChildren;
	double grossIncome, pensionPercent;

	getData(numberOfChildren, grossIncome, pensionPercent);

	return 0;
}
void getData(int& numberOfChildren, double& grossIncome, double& pensionPercent)
{
	status state;
	string maritalStatus;

	cout << "Federal Tax Program" << endl;

	do
	{
		cout << "Please enter your marital status ('Single' or 'Married'): ";
		cin >> maritalStatus;
		cout << endl;

		if (maritalStatus == "Married")
			state = Married;
		else if (maritalStatus == "Single")
			state = Single;
		else
			cout << "Invalid Entery." << endl;
	}
		while (state != Single || state != Married);

	switch (state)
	{
	case Married:
		cout << "Please enter you and your spouce's gross income: ";
		cin >> grossIncome;
		cout << endl;

		cout << "Please enter the number of children you have under the age of 14: ";
		cin >> numberOfChildren;
		cout << endl;

		cout << "Please enter the percentage of your gross income that was put towards a pension plan (0-6): ";
		cin >> pensionPercent;
		cout << endl;
	case Single:
		cout << "Please enter your gross income: ";
		cin >> grossIncome;
		cout << endl;

		cout << "Please enter the percentage of your gross income that was put towards a pension plan (0-6): ";
		cin >> pensionPercent;
		cout << endl;
	}
}




When I enter the correct string "Single" or "Married" the loop doesn't exit. Sorry this is my first forum post of any sort of code and I'm not sure if enclosed it in the code format correctly.
Last edited on
To post code on this forum, simply surround it with code tags -- e.g., [code]int main() { ... }[/code] will produce int main() { ... }

Anyways,
while (state != Single || state != Married);
In pseudocode, that's something like
1
2
ask again if the response is not single; or
ask again if the response is not married

The subject can never be both single and married, so your condition is wrong (always true). You probably want to
ask again if the response is neither single nor married, i.e.,
while (! (state == Married || state == Single));

The key is the English construction "neither... nor" which implies the logical operator NOR, written as NOT (a OR b)
Last edited on
1
2
 
while (! (state == Married || state == Single));


This worked like a charm. When I went back and read what I had before it makes sense that it wouldn't exit the loop with a correct response. Thank you!
Ok, so I finished up the program and instead of calling two separate functions in main() I decided to call the taxAmount function inside of the getData function. Now I'm having another problem with my enum data type and my do while loop. When i enter something other than "Single" or "Married" I encounter a debug error.

"Run-Time Check Failure #3 - The variable 'state' is being used without being initialized."

Here is the whole code:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// User-defined Simple Data Types
enum status {Single, Married};

// Function Prototypes
void getData();
double taxAmount(int numberOfChildren, double grossIncome, double pensionAmount, status state);

// Constant Variables
const int SINGLE_EXEMPTION = 4000;
const int MARRIED_EXEMPTION = 7000;
const int PERSONAL_EXEMPTION = 1500;
const double MAX_PENSION_PERCENT = 0.06;
const int MID_BASE_RATE = 2250;
const int HIGH_BASE_RATE = 8460;
const double MID_TAX_PERCENT = 0.25;
const double HIGH_TAX_PERCENT = 0.35;

int main()
{

	getData();

	return 0;
}
void getData()
{
	int numberOfChildren;
	double totalTaxes, grossIncome, pensionPercent, pensionAmount;
	string maritalStatus;
	status state;

	cout << fixed << showpoint << setprecision(2);
	cout << "Federal Tax Program" << endl;

	do
	{
		cout << "Please enter your marital status ('Single' or 'Married'): ";
		cin >> maritalStatus;
		cout << endl;

		if (maritalStatus == "Married")
			state = Married;
		else if (maritalStatus == "Single")
			state = Single;
		else
			cout << "Invalid Entery." << endl;
	}
		while (! (state == Married || state == Single));

	switch (state)
	{
	case Married:
		cout << "Please enter you and your spouce's gross income: ";
		cin >> grossIncome;
		cout << endl;

		cout << "Please enter the number of children you have under the age of 14: ";
		cin >> numberOfChildren;
		cout << endl;

		do
		{
			cout << "Please enter the percentage of your gross income that was put towards a pension plan (0-6): ";
			cin >> pensionPercent;
			cout << endl;
		} 
		while (! (pensionPercent > 0 && pensionPercent <= 6));
		
		pensionPercent = pensionPercent / 100;
		pensionAmount = grossIncome * pensionPercent;

		break;
	case Single:
		cout << "Please enter your gross income: ";
		cin >> grossIncome;
		cout << endl;

		do
		{
			cout << "Please enter the percentage of your gross income that was put towards a pension plan (0-6): ";
			cin >> pensionPercent;
			cout << endl;
		} while (! (pensionPercent > 0 && pensionPercent <= 6));

		pensionPercent = pensionPercent / 100;
		pensionAmount = grossIncome * pensionPercent;
		break;

	}

	totalTaxes = taxAmount(numberOfChildren, grossIncome, pensionAmount, state);

	if (totalTaxes > 0)
		cout << "The total amount of taxes you owe is $" << totalTaxes << endl;
	else
		cout << "You don't owe any taxes this year." << endl;
}
double taxAmount(int numberOfChildren,double grossIncome,double pensionAmount,status state)
{
	double taxableIncome, totalTax;
	int totalExemption;

	switch (state)
	{
	case Married:
		totalExemption = numberOfChildren + 2;
		taxableIncome = grossIncome - pensionAmount - MARRIED_EXEMPTION - (totalExemption * PERSONAL_EXEMPTION);
		cout << "Your taxable income is $" << taxableIncome << endl;
		break;
	case Single:
		totalExemption = 1;
		taxableIncome = grossIncome - pensionAmount - SINGLE_EXEMPTION - (totalExemption * PERSONAL_EXEMPTION);
		cout << "Your taxable income is $" << taxableIncome << endl;
		break;
	}
	if (taxableIncome < 15000)
		totalTax = taxableIncome * 0.15;
	else if (taxableIncome > 15000 && taxableIncome <= 40000)
		totalTax = ((taxableIncome - 15000) * MID_TAX_PERCENT) + MID_BASE_RATE;
	else
		totalTax = ((taxableIncome - 40000) * HIGH_TAX_PERCENT) + HIGH_BASE_RATE;

	return totalTax;
}
else cout << "Invalid Entery." << endl;
the program warns the user that the entry is invalid but still accepts it, leading to the error that you receive. There should be proper input validation meaning that this part of the program should loop until valid data has been entered, see here for an example just posted: http://www.cplusplus.com/forum/beginner/216035/#msg1002549
I'm familiar with input validation but what troubles me is that I didn't change any code in that section when I switched around my function calls so why would I get this error now and not before when I ran the program?
ran your program couple times, don't notice anything unusual?
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
//Program Run 1: 
Federal Tax Program
Please enter your marital status ('Single' or 'Married'): Divorced

Invalid Entery.
Please enter your marital status ('Single' or 'Married'): Single

Please enter your gross income: 560000

Please enter the percentage of your gross income that was put towards a pension
plan (0-6): 4

Your taxable income is $532100.00
The total amount of taxes you owe is $180695.00

Process returned 0 (0x0)   execution time : 21.123 s
Press any key to continue.

//Program Run 2: 

Federal Tax Program
Please enter your marital status ('Single' or 'Married'): Mrd

Invalid Entery.
Please enter your marital status ('Single' or 'Married'): Married

Please enter you and your spouce's gross income: 560000

Please enter the number of children you have under the age of 14: 2

Please enter the percentage of your gross income that was put towards a pension
plan (0-6): 4

Your taxable income is $524600.00
The total amount of taxes you owe is $178070.00

Process returned 0 (0x0)   execution time : 17.148 s
Press any key to continue. 
Thanks for the heads up there gunner. I can't even get to that part to test it further because of my previous question. I had the program working properly before calling both functions separately instead of amountDue() inside of getData(). I probably should have just left it alone instead of trying to teak it.
So I made the change back to roughly what my previous code was and everything is working fine. I produced the same results you previously posted but I don't see anything wrong with what your produced. This is my working code:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// User-defined Simple Data Types
enum status {Single, Married};

// Function Prototypes
void getData(int& numberOfChildren, double& grossIncome, double& pensionAmount, status& state);
void taxAmount(int numberOfChildren, double grossIncome, double pensionAmount, status state);

// Constant Variables
const int SINGLE_EXEMPTION = 4000;
const int MARRIED_EXEMPTION = 7000;
const int PERSONAL_EXEMPTION = 1500;
const double MAX_PENSION_PERCENT = 0.06;
const int MID_BASE_RATE = 2250;
const int HIGH_BASE_RATE = 8460;
const double MID_TAX_PERCENT = 0.25;
const double HIGH_TAX_PERCENT = 0.35;

int main()
{
	int numberOfChildren;
	double grossIncome, pensionAmount;
	status state;

	getData(numberOfChildren, grossIncome, pensionAmount, state);

	taxAmount(numberOfChildren, grossIncome, pensionAmount, state);

	return 0;
}
void getData(int& numberOfChildren, double& grossIncome, double& pensionAmount, status& state)
{
	double pensionPercent;
	string maritalStatus;


	cout << fixed << showpoint << setprecision(2);
	cout << "Federal Tax Program" << endl;

	do
	{
		cout << "Please enter your marital status ('Single' or 'Married'): ";
		cin >> maritalStatus;
		cout << endl;

		if (maritalStatus == "Married")
			state = Married;
		else if (maritalStatus == "Single")
			state = Single;
		else
			cout << "Invalid Entery." << endl;
	}
		while (! (state == Married || state == Single));

	switch (state)
	{
	case Married:
		cout << "Please enter you and your spouce's gross income: ";
		cin >> grossIncome;
		cout << endl;

		cout << "Please enter the number of children you have under the age of 14: ";
		cin >> numberOfChildren;
		cout << endl;

		do
		{
			cout << "Please enter the percentage of your gross income that was put towards a pension plan (0-6): ";
			cin >> pensionPercent;
			cout << endl;
		} 
		while (! (pensionPercent > 0 && pensionPercent <= 6));
		
		pensionPercent = pensionPercent / 100;
		pensionAmount = grossIncome * pensionPercent;

		break;
	case Single:
		cout << "Please enter your gross income: ";
		cin >> grossIncome;
		cout << endl;

		do
		{
			cout << "Please enter the percentage of your gross income that was put towards a pension plan (0-6): ";
			cin >> pensionPercent;
			cout << endl;
		} while (! (pensionPercent > 0 && pensionPercent <= 6));

		pensionPercent = pensionPercent / 100;
		pensionAmount = grossIncome * pensionPercent;
		break;

	}
}
void taxAmount(int numberOfChildren,double grossIncome,double pensionAmount,status state)
{
	double taxableIncome, totalTaxes;
	int totalExemption;

	switch (state)
	{
	case Married:
		totalExemption = numberOfChildren + 2;
		taxableIncome = grossIncome - pensionAmount - MARRIED_EXEMPTION - (totalExemption * PERSONAL_EXEMPTION);
		cout << "Your taxable income is $" << taxableIncome << endl;
		break;
	case Single:
		totalExemption = 1;
		taxableIncome = grossIncome - pensionAmount - SINGLE_EXEMPTION - (totalExemption * PERSONAL_EXEMPTION);
		cout << "Your taxable income is $" << taxableIncome << endl;
		break;
	}
	if (taxableIncome < 15000)
		totalTaxes = taxableIncome * 0.15;
	else if (taxableIncome > 15000 && taxableIncome <= 40000)
		totalTaxes = ((taxableIncome - 15000) * MID_TAX_PERCENT) + MID_BASE_RATE;
	else
		totalTaxes = ((taxableIncome - 40000) * HIGH_TAX_PERCENT) + HIGH_BASE_RATE;

	if (totalTaxes > 0)
		cout << "The total amount of taxes you owe is $" << totalTaxes << endl;
	else
		cout << "You don't owe any taxes this year." << endl;
}
Topic archived. No new replies allowed.