Loops 为布尔值输入无效值会导致infin错误。循环并阻止我输入更多信息

Loops 为布尔值输入无效值会导致infin错误。循环并阻止我输入更多信息,loops,boolean,Loops,Boolean,在这里,我试着做一个基本的计算,只要用户愿意,它就可以添加整数。我遇到了一个问题,如果我为bool输入一个无效的值,程序将无限地运行循环。 虽然如果用户没有输入无效值也不是问题, 但如果代码万无一失,那就太好了 #include <iostream> #include <cmath> using namespace std; int main () { // here i set up the code to create a calculator that

在这里,我试着做一个基本的计算,只要用户愿意,它就可以添加整数。我遇到了一个问题,如果我为bool输入一个无效的值,程序将无限地运行循环。 虽然如果用户没有输入无效值也不是问题, 但如果代码万无一失,那就太好了

#include <iostream>
#include <cmath>

using namespace std;



int main ()
{
   // here i set up the code to create a calculator that will add  
  //  as long as the user wants.
    int Num1 ;
    int Num2 ;
    bool Permit = false;
    cout<< "enter first number:"<<endl;
    cin>> Num1;
    cout<< "Do you wish to continue? 1 for yes 0 for no." <<endl;
    cin>> Permit;
    
   // here the loop should run only as long as the bool is true. 

    while (Permit)
    {
        cin>> Num2;
        Num1 = Num2+Num1;
        cout<< "Do you wish to continue? 1 for yes 0 for no." <<endl;
        cin>> Permit;

    }
    cout <<Num1;


    // however if i enter a value other than 0 or 1 , the whole  
    // loop runs infinitely , but doesnt allow me to enter any 
    // info.
    // if i enter any alphabet instead of 0 or 1, the opposite 
    // happens the loop terminates immediately.
}
#包括
#包括
使用名称空间std;
int main()
{
//在这里,我设置了创建计算器的代码,该计算器将
//只要用户愿意。
int Num1;
int Num2;
bool-Permit=false;
coutnum2;
Num1=Num2+Num1;
库特