【发布时间】:2015-08-04 03:15:47
【问题描述】:
第一次海报。我正在编写一个程序来计算联邦所得税。用户将输入状态码 (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;
}
【问题讨论】:
-
你能发布你从代码中得到的错误吗?这可以帮助我们找出代码中可能存在的错误。
-
这些是我得到的错误代码,1>MSVCRTD.lib(crtexe.obj):错误 LNK2019:未解析的外部符号 _main 在函数 ___tmainCRTStartup 中引用:致命错误 LNK1120:1 个未解析的外部
-
编写代码的方法是从小而简单的完美工作开始,然后逐渐增加一点复杂性,在每一步都进行测试,并且永远不要添加不适合的代码' t 工作。 在这种情况下,你有一个链接错误,所以你应该尝试编写一些简单的东西,包括链接到一个库(或者你想要做的任何事情)。小步骤。
-
总是看first错误,否则你会走错方向。我想这个链接错误是尝试链接由于早期错误而未构建的对象的结果。例如,您发布的代码有像
std:int这样的语法错误,以及很容易出现嵌套错误(未经验证,但代码存在严重的布局问题,很容易导致这种情况)和其他疯狂的事情,比如使用整个范围块作为返回值。 -
我认为链接器错误是一个红鲱鱼。我的代码出现大量编译器错误。例如,
std:int是什么?另外,cin >> statusCode = 9;是什么?这些都不是有效的语法。