Arrays and Functions

[
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

int getStatusCode();
float calcTaxAmt();
void displayResults();
float getTaxIncome();


std:int getStatusCode()
{
std:string filingStatus[4] = { "Single", "Married Filing Jointly", "Married filing Seperately", "Head of Household" };
bool statusCode = false;

do {
cout << "Enter your filing status: " << endl;
for (int i = 0; i < filingStatus[4]); i++;)
{
cout << "Enter '" << i + 1 << "' for " << filingStatus[i] << "." << endl;
}
cin >> statusCode;
cout << "\nWhen you are finished, please press '9'.\n" << endl;
cin >> statusCode = 9;

while (statusCode != 9) {

while (statusCode < 1 || (statusCode > 4 && statusCode != 9));

do {
cout << "Enter your filing status: " << endl; // Prompt user to enter their filing status.
cout << "Enter '1' for Single." << endl;
cout << "Enter '2' for Married filing Jointly." << endl;
cout << "Enter '3' for Married filing Seperately." << endl;
cout << "Enter '4' for Head of Household.\n" << endl;
cin >> statusCode;
cout << "\nPlease enter a filing valid filing status.\n" << endl;
}

if (statusCode != 9) {
do {

cout << "\nEnter your income for the past year: \n" << endl; // do while statement to input income
cin >> taxableIncome;

if (taxableIncome < 500) {
cout << "\nYour income is less than $500. You do not owe any taxes.\n" << endl; // message if user's income is less than $500
}

} while (taxableIncome < 500);

return;
{

float calcTaxAmnt(int statusCode, float taxableIncome) {
float taxAmt;

switch (statusCode) // switch statement to compute taxes owed
{

case 1: // Filing single
if (taxableIncome <= 7550) {
taxAmt = (taxableIncome)* (float)(.10);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 7550 && taxableIncome < 30650) {
taxAmt = 7550 * (float)(.10) + (taxableIncome - 7550) * (float)(.16);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 30650 && taxableIncome < 74200) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (taxableIncome - 30650) * (float)(.26);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 74200 && taxableIncome < 154800) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (74200 - 30650) * (float)(.26) + (taxableIncome - 74200)* (float)(.30);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 154800 && taxableIncome < 336550) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (74200 - 30650) * (float)(.26) + (154800 - 74200) * (float)(.30) + (taxableIncome - 154800) * (float)(.33);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 336550) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (74200 - 30650) * (float)(.26) + (154800 - 74200) * (float)(.30) + (336550 - 154800) * (float)(.33) + (taxableIncome - 336550)* (float)(.38);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;

}


break;


case 2: // Married filing jointly
if (taxableIncome <= 15100) {
taxAmt = (taxableIncome)* (float)(.10);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 15100 && taxableIncome < 61300) {
taxAmt = 15100 * (float)(.10) + (taxableIncome - 15100) * (float)(.16);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 61300 && taxableIncome < 123700) {
taxAmt = 15100 * (float)(.10) + (61300 - 15100)* (float)(.16) + (taxableIncome - 61300)* (float)(.26);
cout << "\nThe amount of taxes you owe is:" << taxAmt << endl;
}
else if (taxableIncome > 123700 && taxableIncome < 188450) {
taxAmt = 15100 * (float)(.10) + (61300 - 15100)* (float)(.16) + (123700 - 61300)*(float)(.26) + (taxableIncome - 123700)* (float)(.30);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 188450 && taxableIncome < 336550) {
taxAmt = 15100 * (float)(.10) + (61300 - 15100)* (float)(.16) + (123700 - 61300)*(float)(.26) + (188450 - 123700) + (taxableIncome - 188450)* (float)(.33);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 336550) {
taxAmt = 15100 * (float)(.10) + (61300 - 15100)* (float)(.16) + (123700 - 61300)*(float)(.26) + (188450 - 123700) + (336550 - 188450) * (float)(.33) + (taxableIncome - 336550)* (float)(.38);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
break;


case 3: // Married filing seperately
if (taxableIncome <= 7550) {
taxAmt = taxableIncome * (float)(.10);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 7550 && taxableIncome < 30650) {
taxAmt = 7550 * (float)(.10) + (taxableIncome - 7550) * (float)(.16);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 30650 && taxableIncome < 61850) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (taxableIncome - 30650) * (float)(.26);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 61850 && taxableIncome < 94225) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (61850 - 30650) * (float)(.26) + (taxableIncome - 61850) * (float)(.30);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 94226 && taxableIncome < 168275) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (61850 - 30650) * (float)(.26) + (94226 - 61850) * (float)(.30) + (taxableIncome - 94226) * (float)(.33);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 168276) {
taxAmt = 7550 * (float)(.10) + (30650 - 7550) * (float)(.16) + (61850 - 30650) * (float)(.26) + (94226 - 61850) * (float)(.30) + (168276 - 94226) * (float)(.33) + (taxableIncome - 168276)* (float)(.38);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
break;


case 4: // Head of household
if (taxableIncome <= 10750) {
taxAmt = taxableIncome * (float)(.10);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 10750 && taxableIncome < 41050) {
taxAmt = 10750 * (float)(.10) + (taxableIncome - 10750)* (float)(.16);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 41050 && taxableIncome < 106000) {
taxAmt = 10750 * (float)(.10) + (41050 - 10750)* (float)(.16) + (taxableIncome - 41050)* (float)(.26);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 106000 && taxableIncome < 171650) {
taxAmt = 10750 * (float)(.10) + (41050 - 10750)* (float)(.16) + (106000 - 41050)* (float)(.26) + (taxableIncome - 106000)* (float)(.30);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 171650 && taxableIncome < 336550) {
taxAmt = 10750 * (float)(.10) + (41050 - 10750)* (float)(.16) + (106000 - 41050)* (float)(.26) + (171650 - 106000)* (float)(.30) + (taxableIncome - 171650)* (float)(.33);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
else if (taxableIncome > 336550) {
taxAmt = 10750 * (float)(.10) + (41050 - 10750)* (float)(.16) + (106000 - 41050)* (float)(.26) + (171650 - 106000)* (float)(.30) + (336550 - 171650)* (float)(.33) + (taxableIncome - 336550)* (float)(.38);
cout << "\nThe amount of taxes you owe is: " << taxAmt << endl;
}
break;

}


}
return taxAmt;

}

int main() {

int statusCode; // variables
float taxableIncome;
float taxAmt;
float taxRate[6] = { .10, .16, .26, .30, .33, .38 }; // tax rate array

void displayResults()



system("pause");
return 0;


}

]
Here is my question: I am writing a program to calculate federal income tax. The user is to enter a status code (1,2,3,4) to represent a filing status. I am to write a function for this, to get the tax income, to calculate the tax amount and to display the results. I also am using to two one dimensional arrays for the tax rates and the filing status. What I have written so far is posted. Any help is appreciated. Why am I getting the errors? How can I fix the functions and arrays?
Hello tbirdlx89,

Please edit your original post and use code tags. Without them, your code is hard to read, and it makes people less likely to help you. It helps us tremendously because we can easily identify code line numbers and view your code with proper formatting.

How to use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

At a cursory glance, I can see the following problems with your code:

1.) The only reason std:string doesn't come up as an error is because of using namespace std; - It will compile but it is technically incorrect - The scope resolution operator is a double colon, and should therefore be std::string.

2.) The same is true for std:int which would become std::int, but that's not a valid type in the std namespace anyway.

3.) There's several problems with this for-loop control structure: for (int i = 0; i < filingStatus[4]); i++;). You have an unnecessary (and wrong) trailing closing parenthesis as well as a semi-colon. Those should be removed. In addition, you're comparing an integer (int i) with a std::string (filingStatus[4]). I'm sure what you meant to do was for(int i =0; i < 4; i++) - even then the use of a "magic number" such as four is not encouraged. I suggest a constant integer to represent the size of the array:

1
2
3
4
5
6
const int num_status_strings = 4;
std::string filingStatus[num_status_strings] = {/*...*/}

for(int i = 0; i < num_status_strings; i++) {
/**/
}


EDIT - I popped your code into my IDE, I see a lot of unmatched brackets and logical errors. It may sound like a large task, but I suggest starting over, which I'm prepared to help you with.

I've made a little template for you to start out with. I was able to deduce some of the parameters for your functions based on the code you provided, though I'm sure the function signatures aren't quite complete yet. The functions also don't do anything so far. If you have any questions feel free to ask - If you can tell me more about what you're envisioning (such as the flow of the program or what results you're expecting), then I'll be able to help you more. Hope this helps.

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

int getStatusCode() {
	/**/
}

float getTaxIncome(/**/) {
	/**/
}

float getTaxAmount(int _code, float _taxable) {
	/**/
}

void displayResults(/**/) {
	/**/
}

int main(int argc, char* argv[]) {

	int status_code = getStatusCode();
	float tax_income = getTaxIncome(/**/);
	float tax_amount = getTaxAmount(status_code, tax_income);

	displayResults(/**/);

	//"Pause" the program
	return 0;
}
Last edited on
Topic archived. No new replies allowed.