C ++程序在应终止时重复

问题描述

我一直在尝试制作C ++“数学导师”程序。目的是询问用户是否要进行加法,减法或乘法,一旦他们做出选择并且计算完成,就会提示他们键入0终止程序,并键入1重复该程序。 我尝试为此使用do-while循环,但是当我运行它时,我发现只要用户按下0或1,程序就会重复执行,并且永远不会终止。 这是代码(没有数学部分,因为这确实无关紧要):

    #include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

/*
 * program to serve as math tutor for a young student
 */
int main(int argc,char** argv) {
    
    int repeatChoice = 0;
    //actual program has more variables here,but those are all math variables.
    
    do {
        //in this area was just code for the actual math part
        cout << "Do you want to repeat this program (0 for no,1 for yes)?" << endl;
        cin >> repeatChoice;
        while (repeatChoice != 0 && repeatChoice != 1) {
            cout << "Invalid input. Please select 0 or 1." << endl;
            cin >> repeatChoice;
        }
        if (repeatChoice = 0)
            break;
    } while (repeatChoice = 1);

    return 0;
}

如果有人认为看到完整的代码会有所帮助,我将编辑问题以添加它。 请注意,我是编程的新手,还没有真正研究任何复杂的概念,所以请您以相对简单的术语进行解释。

解决方法

从我可以看到的问题出在代码的这一部分:

...
if (repeatChoice = 0)
            break;
    } while (repeatChoice = 1);
...

您正在使用赋值运算符=而不是比较运算符==