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

C++ 创建一个while循环,返回C+中的(y/n)语句+;,c++,while-loop,do-loops,C++,While Loop,Do Loops,所以我的问题是,我试图创建一个税务程序,获取用户输入的收入、状态,然后打印出结果。我可以毫无差错地完成这一切。一旦我尝试将其放入while循环中,该循环将提示用户“是否要再次计算?”用户可以输入y/n。“n”完成程序,“y”需要重复程序,用户再次输入信息等。我无法让程序允许用户为变量输入新值。它只会重复结果。有人能帮我弄清楚吗?我已经尝试了我能做的一切,并且检查了我能做的事情和其他人做的事情,这些都不适合我。感谢您的帮助 #include <iostream> #include &l

所以我的问题是,我试图创建一个税务程序,获取用户输入的收入、状态,然后打印出结果。我可以毫无差错地完成这一切。一旦我尝试将其放入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;

}
#包括
#包括
使用名称空间std;
双收入,国际收支平衡;
木炭海洋状态;
char再次='Y';
int main(){
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
计算精度(2);
收入=-1;
//应税单身人士
常数双ts1=863;
常数双ts2=2588;
常数双ts3=4313;
//应税婚姻
常数双tm1=1726;
常数双tm2=5176;
常数双tm3=8626;
//税率
常数双tr1=.023;
常数双tr2=.033;
常数双tr3=.052;
常数双tr4=.075;
双t1,t2,t3;
//添加变量
常数双as1=25,as2=85,as3=181;
常数双am1=40,am2=175,am3=390;
//加法变量简化
双a1、a2、a3;
//常量字符串
常量字符串s=“单个”,m=“关节”;
字符串状态;
int i=0;
//循环开始
while(再次=='y'| |再次=='y'){
而(收入<0){

cout您可能应该有一个单独的函数,如
void doTaxes()
包含您的业务逻辑,然后在
main()
中,您可以在条件为true时进行调用。您可以在doTaxes内将条件变量设置为false,或者更好地设置doTaxes()使用int或bool类型,并根据返回值设置条件变量

bool isTaxLoop(true);

void doTaxes(...);

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

您必须为第二次通过重置收入,如下所示

income = -1;
while(again == 'y' || again == 'Y') 
{
    while(income < 0) 
    {
        ...
    }
    ...
    cout << "Go again? (y/n) ";
    cin >> again; 
    income = -1; //< -- reset income ****
}// end loop
income=-1;
while(再次=='y'| |再次=='y')
{
而(收入<0)
{
...
}
...
又不能了;
收入=-1;/<--重置收入****
}//端环

手动逐步完成代码。如果你写下变量的值,你会很容易发现问题。你应该将整个过程简化为最基本的循环(即删除所有不要求用户再次执行的内容)。这里的代码太多了,无法查看。如果您只有10行代码,您可能会自己找到问题,或者至少其他人会更容易找到问题。您可以随时查看您的问题。我在查看发生的情况后能够找到问题。尽管由于某些原因,它无法正确循环。。。。