循环失败

The*_*mer 2 c++ visual-c++

我需要让程序循环,直到用户输入"XXXX"作为名称.问题是,我的代码最多只循环一次并退出.我已经尝试过做一整天......而且似乎无法解决这个问题,但我觉得我真的很亲密!

编辑: 你们真棒!做了两个更改但现在它跳过了名称提示并直接进入存款提示.如何让它在循环中包含名称提示?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main() {

string name, XXXX;
float deposit, time, interest, total, totinterest;
int compound;

cout << left;

int flag = 0;

do {

    cout << "Enter your name or XXXX to stop: ";
    getline(cin, name);
    cout << name << " enter the following information: " << endl;


    if (name == "XXXX"){
        flag = 1;
    }else 
    {

        cout << setw(60) << "\tAmount on deposit: ";
        cin >> deposit;
        if (!deposit || deposit < 0){
            cout << "\tPlease enter a positive number! ";

        }

        cout << setw(60) << "\tYears on deposit: ";
        cin >> time;
        if (!time || time < 0){
            cout << "\tPlease enter a positive number! ";
            return(0);
        }

        cout << setw(60) << "\tNumber of times the interest is compounded per year: ";
        cin >> compound;
        if (!compound || compound < 0){
            cout << "\tPlease enter a positive number! ";
            return(0);
        }

        if (time >= 5)
            interest = .045;
        else if (time >= 4)
            interest = .04;
        else if (time >= 3)
            interest = .035;
        else if (time >= 2)
            interest = .025;
        else if (time >= 1)
            interest = .02;
        else if (time < 1)
            interest = .015;

        total = (deposit)*pow((1 + interest / compound), (compound * time));
        totinterest = total - deposit;

        cout << left << setprecision(2) << fixed;
        cout << endl;
        cout << setw(15) << "Name " << setw(14) << "Years" << setw(18) << "Deposit Amount" << setw(18) << "Interest Earned " << setw(18) << "Total" << endl;
        cout << "===============================================================================" << endl;
        cout << setw(15) << name << setw(14) << time << setw(1) << "$" << setw(17) << deposit << setw(1) << "$" << setw(17) << totinterest << setw(1) << "$" << setw(18) << total << endl;
        cout << endl;
        cout << "Thank you for using tax program. Have a nice day. " << endl;
        return(0);

    }

} while (flag = 0);

}
Run Code Online (Sandbox Code Playgroud)

rm4*_*rm4 5

return(0)会让你脱离主力.删除该行并重试.


sha*_*ats 5

return(0)将退出main(),程序将终止.因此,将其从源中删除.

此外条件while (flag = 0)将始终返回false.将其更改为while (flag == 0)否则会导致循环仅迭代一次.

最后,你需要<math.h>这个pow()功能.

#include <math.h>
Run Code Online (Sandbox Code Playgroud)