c++帮助数组和函数
C++ Help Array and Function
第一次海报。我正在写一个计算联邦所得税的程序。用户将输入状态码(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;
}
你发布的错误正在发生,因为编译器找不到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上发布。
相关文章:
- 第 i 个元素返回 i 的函数数组
- 使用嵌套函数数组是个好主意吗?
- 正确实现类函数数组
- 试图引用已删除函数数组的相等运算符
- 如何在静态函数中使用成员函数数组
- 无法创建函数数组
- 构造函数数组
- 使用开关或定义函数数组 - 更快
- 编写一个函数,该函数将覆盖主函数数组中的值
- 可以像'funs[1]();'一样调用的函数数组
- std::AVX内部函数数组
- 如果数组大小未知,如何将函数数组作为参数传递
- 从函数数组中获取值
- 使用元编程在编译时初始化函数数组
- 将C函数数组转换为C++函数指针数组
- 复制构造函数 [数组] C++
- 添加函数数组并随机选择其中一个
- 指针到私有成员函数数组
- 可以单独声明函数数组的元素
- (c++)将main中的随机值保存到函数数组中