我正在制作关于Cramer规则和矩阵的程序(太多空闲时间)。在某个地方,我有这个问题,你需要输入“是”或“否”。如果我输入其他单词(例如lol),它将不会转到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 ++的初学者 使用代码块
答案 0 :(得分:4)
阅读do while循环!这是一个后测试循环,因此您可以验证z是或否。 http://www.cplusplus.com/doc/tutorial/control/
基本思路是: 当用户输入不等于是或否时,用户输入。
基本示例:
int x;
do {
cout << "Please enter 1 or 0" << endl;
cin >> x;
} while (x != 0 && x != 1);