【问题标题】:C++ Help Array and FunctionC++ 帮助数组和函数
【发布时间】: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 &gt;&gt; statusCode = 9; 是什么?这些都不是有效的语法。

标签: c++ arrays function


【解决方案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 上。

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多