C++ y/n响应后保存while循环信息

C++ y/n响应后保存while循环信息,c++,while-loop,C++,While Loop,我的代码有问题,但我不知道如何修复它 我用DEV-C++做了C++的计算器。我做了一个while循环,这样用户就不必重新启动程序来再次使用它。我试图添加在下一次计算中使用之前计算的答案的功能,但代码被跳过 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { string username; float num1, n

我的代码有问题,但我不知道如何修复它

我用DEV-C++做了C++的计算器。我做了一个
while
循环,这样用户就不必重新启动程序来再次使用它。我试图添加在下一次计算中使用之前计算的答案的功能,但代码被跳过

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    string username;
    float num1, num2, answer;
    string berekening;
    string again;
    float oldanswer;
    string oldanswerq;

    again = "y";

    cout << "Hello who are you?" << endl;
    cout << "" << endl;
    cin >> username;

    cout << "" << endl;
    cout << "well hello " << username << endl;
    cout << "" << endl;
    while (again == "y"){

    oldanswer = answer;
        if (oldanswer == 0)    {
            cout << "what is the first number you wanna put in " << username << endl;
            cout << "" << endl;
            cin >> num1;
        }

        else {
            cout << "do you wanna use your old answer? y/n" << endl;
            cout << "" << endl;
            cin >> oldanswerq;
        }

        cout << "" << endl;
        cout << "+, -, x or ÷(u can use / instead of ÷" << endl;
        cout << "" << endl;
        cin >> berekening;

        cout << "" << endl;
        cout << "and what is the second number " << username << endl;
        cout << "" << endl;
        cin >> num2;

        cout << "" << endl;
        if (berekening == "+"){
            answer = num1 + num2;
        }

        else if (berekening == "-"){
            answer = num1 - num2;
        }

        else if (berekening == "x"){
            answer = num1 * num2;
        }

        else if (berekening == "/"){
            answer = num1 / num2;
        }

        else if (berekening == "÷"){
            answer = num1 / num2;
        }

        cout << username << ", you choosed " << berekening << " what i did was: " << num1 << berekening << num2 << "=" << answer << endl;
        cout << "" << endl;
        cout << username << ", do you wanna go again? y/n" << endl;
        cout << "" << endl;
        cin >> again;
        cout << "" << endl;
    }
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
字符串用户名;
浮点数num1,num2,回答;
弦乐演奏;
再串一次;
浮点数;
字符串oldanswerq;
再次=“y”;

不能在循环外部将
oldanswer
设置为等于
newanswer
。这应该在循环内部完成


在比较
浮点值时,您也不应该使用
==
,因为它们很少完全等于某个值(小数只能精确到计算机上的某些位置)。

使您的解释更加清晰,并修复了代码格式和缩进。