创建一个在C ++中返回(y / n)语句的while循环

时间:2018-01-22 05:11:29

标签: c++ while-loop do-loops

所以我的问题是我正在尝试创建一个税收计划,该计划获取用户对收入,状态的输入,然后打印出结果。我可以完成所有这一切,没有任何错误。一旦我尝试将其置于while循环中,它将提示用户"你想再次计算吗?"用户可以输入y / n。 " N"完成该计划和" y"需要重复该程序,用户再次输入信息,等等。我无法让程序让用户为变量输入新值。它只会重复结果。有人可以帮我解决这个问题吗?我尽我所能,并检查了我能做什么以及别人做了什么,对我没有用。任何帮助表示赞赏!

#include <iostream>
#include <string>
using namespace std;
double income, incomeTax;
char maritalStatus;
char again = 'Y';




int main() {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    income = -1;
    //Taxable Singles
    const double ts1 = 863;
    const double ts2 = 2588;
    const double ts3 = 4313;
    //Taxable Marriage
    const double tm1 = 1726;
    const double tm2 = 5176;
    const double tm3 = 8626;
    //Tax Rates
    const double tr1 = .023;
    const double tr2 = .033;
    const double tr3 = .052;
    const double tr4 = .075;
    double t1, t2, t3;
    //Add variable
    const double as1 = 25, as2 = 85, as3 = 181;
    const double am1 = 40, am2 = 175, am3 = 390;
    //Addition variable simplified
    double a1, a2, a3;
    //const strings
    const string s = "single", m = "joint";
    string status;

    int i = 0;

    //Beginning of Loop
    while (again == 'y' || again == 'Y') {

        while (income < 0) {
            cout << "Please enter your taxable Income." << "\n (This must be a positive value): " << endl;
            cin >> income;

            if (cin.fail() || income < 0) {
                cout << "Please try again. Needs to be a positive number." << endl;
                cin.clear();
                cin.ignore(40, '\n');
                income = -1;
            }
        }
        while (maritalStatus == false) {
            cout << "Please enter m if married and filing joint return," <<
                "\n or s if filing a single return: ";
            cin >> maritalStatus;

            if (maritalStatus != 's' && maritalStatus != 'm') {
                cout << "Please try again. Needs to be a 's' or 'm'." << endl;
                cin.clear();
                cin.ignore(40, '\n');
                maritalStatus = false;
            }
        }
        if (maritalStatus == 's') {
            t1 = ts1;
            t2 = ts2;
            t3 = ts3;
            a1 = as1;
            a2 = as2;
            a3 = as3;
            status = s;

        }
        else if (maritalStatus == 'm') {
            t1 = tm1;
            t2 = tm2;
            t3 = tm3;
            a1 = am1;
            a2 = am2;
            a3 = am3;
            status = m;
        }
        if (income > 0 && income <= t1) {
            incomeTax = (income - (0)) * tr1;
        }
        else if (income > t1 && income <= t2) {
            incomeTax = (income - (t1 - 1)) * tr2 + a1;
        }
        else if (income > t2 && income <= t3) {
            incomeTax = (income - (t2 - 1)) * tr3 + a2;
        }
        else if (income > t3) {
            incomeTax = (income - (t3 - 1)) * tr4 + a3;
        }

        cout << "Your taxable income is " << "$" << income << endl;
        cout << "and you're filing a" << status << " return." << endl;
        cout << "Your income tax will be " << "$" << incomeTax << endl;

        cout << "Go again? (y/n) ";
        cin >> again; //change control variable

    }// end loop

        system("pause");
        return 0;

}

2 个答案:

答案 0 :(得分:1)

您可能应该有一个单独的功能,例如void doTaxes()包含您的业务逻辑,然后在main()中您可以在条件为真时拨打电话。您可以在doTaxes中将条件变量设置为false,或者甚至更好地将doTaxes()设置为int或bool类型,并根据返回值设置条件变量。

bool isTaxLoop(true);

void doTaxes(...);

int main()
{
    while(isTaxLoop)
    {
        doTaxes();
    }
    return EXIT_SUCCESS;
}

答案 1 :(得分:0)

您必须重置第二遍的收入,如下所示。

income = -1;
while(again == 'y' || again == 'Y') 
{
    while(income < 0) 
    {
        ...
    }
    ...
    cout << "Go again? (y/n) ";
    cin >> again; 
    income = -1; //< -- reset income ****
}// end loop