2016-09-18 144 views
-2

我正在制作一个关于cramer规则和矩阵的程序(太多空闲时间)。而在某个地方,我有这个问题,您需要输入“是”或“否”。如果我输入其他词语(例如lol),那么如果陈述或再次提出问题,则不会去其他词语。我如何限制答案只有是/否?C++ If/else if语句

cout << "There! you have now your first equation! is it " << a << "x+" << b << "y=" << e << "? Is it? Yes/No"<<endl; 
cin >> z; 
if (z== "No") 
    { 
     while (z== "No") 
      { 
       cout << "what is the value of x?"<<endl; 
       cin >> a; 
       cout << "\n"<<endl; 
       cout << "what is the value of y?"<<endl; 
       cin >> b; 
       cout << "\n"<<endl; 
       cout << "what is the value of answer?"<<endl; 
       cin >> e; 
       cout << "\n"<<endl; 
       cout << "There! you have corrected your first equation! is it " << a << "x+" << b << "y=" << e << "? Is it? Yes/No"<<endl; 
       cin >> z; 
      } 
    } 
else if (z== "Yes") 
    { 
     cout << "We will now proceed!"<<endl; 
    } 

我只是个初学者在C++中使用 代码块

+1

而接受输入你无法控制的输入值。您必须先接受输入,然后才能检查接受的输入是否正确。您可以为错误的输入生成警告。 – Raman

+0

'z'的类型是什么? –

回答

4

在while循环做阅读了!这是一个后期测试循环,因此您可以验证z是yes还是no。 http://www.cplusplus.com/doc/tutorial/control/

其基本思想是: 在用户输入不等于yes或no时执行用户输入。

基本例如:

int x; 
do { 
    cout << "Please enter 1 or 0" << endl; 
    cin >> x; 
} while (x != 0 && x != 1);