这段代码有逻辑错误吗?

时间:2017-01-25 06:41:12

标签: c++

我是编程新手,我刚开始学习C ++。我正在努力制定一个确定一个人税后净收入的计划;但是,该程序无法正常工作。它编译并运行,但它在询问"年度费用后结束。"

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{

    float grossIncome, expenses, adjustedGIncome, taxRate, taxAmount, netIncome; //Declaration of variables
    char again = 'y';
    while(again == 'y') //while loop used to rerun the program without having to recompile
    {
        system("reset");
        cout << "\nEnter your Gross Annual Income: "; //User enters his gross income
        cin >> grossIncome;
        cout << "\nEnter your annual expenses: "; //User enters his annual expenses
        cin >> expenses;

        adjustedGIncome = grossIncome - expenses; //gross income adjusted for taxes
        return adjustedGIncome;

        if (adjustedGIncome >= 415050) //if and else if statements used to determine tax percentage
        {
            taxRate = 0.396;
            return taxRate;
        }
        else if (adjustedGIncome >= 413350 && adjustedGIncome < 415050)
        {
            taxRate = 0.35;
            return taxRate;
        }
        else if (adjustedGIncome >= 190150 && adjustedGIncome < 413350)
        {
            taxRate = 0.33;
            return taxRate;
        }
        else if (adjustedGIncome >= 91150 && adjustedGIncome < 190150)
        {
            taxRate = 0.28;
            return taxRate;
        }
        else if (adjustedGIncome >= 37650 && adjustedGIncome < 91150)
        {
            taxRate = 0.25;
            return taxRate;
        }
        else if (adjustedGIncome >= 9275 && adjustedGIncome < 37650)
        {
            taxRate = 0.15;
            return taxRate;
        }
        else
        {
            taxRate = 0.1;
            return taxRate;
        }

        taxAmount = adjustedGIncome * taxRate; //tax amount determined so that the net income can be determined
        netIncome = adjustedGIncome - taxAmount; 

        cout << "\nAdjusted Gross Income: " << adjustedGIncome; //displays adjusted gross income
        cout << "\nTax Rate: " << taxRate; //displays tax rate depending on adjusted gross income
        cout << "\nTax Amount: " << taxAmount; //displays tax amount
        cout << "\n\nNet Income: " << netIncome; //displays net income
        cout << "\n\nRun this program again? (Y or N): "; //allows user to rerun program
        cin >> again;
        again =tolower(again);
    }
    system("reset");
}

1 个答案:

答案 0 :(得分:1)

您在代码中错误地使用了return语句。 return语句将控制从函数传递回任何调用它的函数。在这种情况下,您将退出主要功能。

当您到达此行return adjustedGIncome;时,您的程序将退出,并且永远不会超越此点。删除此行以及在return树的每个分支中找到的类似if/else语句,以确定税率。全部删除它们。