第一次发表海报。我正在编写一个计算联邦所得税的计划。用户输入状态代码(1,2,3,4)以表示归档状态。我要为此编写一个函数,以获得税收收入,计算税额并显示结果。我还使用两个一维数组来计算税率和申请状态。我到目前为止所写的内容已经发布。任何帮助表示赞赏。 为什么我会收到错误?如何修复函数和数组?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int getStatusCode(); // function declaration
float calcTaxAmt();
void displayResults();
float getTaxIncome();
std:int getStatusCode() // status code function and array
{
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 < size(filingStatus); 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));
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 calcTaxAmount(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
system("pause");
return 0;
}
答案 0 :(得分:1)
您发布的错误正在发生,因为编译器无法找到main。发生这种情况是因为您打开了比关闭时更多的花括号。此外,我不会过多地谈论你的编码风格,但这里还有一些可怕的错误:
std:int getStatusCode()
这里不需要std:
,无论应该做什么。
std:string
这应该是std :: string,它是命名空间,而不是goto标签或其他东西。您甚至在开头使用了using namespace std;
。
由于您没有关闭所有花括号,因此可能会发生其余错误。另请注意,您提供了
的声明void displayResults();
float getTaxIncome();
但您没有提供实施。
这也没有任何意义,我也不知道你在那里做什么:
cin >> statusCode = 9;
第46行还有一个没有分号的返回。
while (statusCode < 1 || (statusCode > 4 && statusCode != 9));
这个while循环没有正文...
for (int i = 0; i < size(filingStatus); i++;)
对于数组也没有size
函数,只需使用4,它是一个简单的数组,你知道它的大小。在i ++之后不应该有分号。
搜索更多错误没有任何意义,从一个简单的项目开始并学习C ++的基础知识。这段代码只是一团糟,不应该在stackoverflow上发布。