c++帮助数组和函数

C++ Help Array and Function

本文关键字:函数 数组 帮助 c++      更新时间:2023-10-16

第一次海报。我正在写一个计算联邦所得税的程序。用户将输入状态码(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上发布。