C++ If/else if statements

C++ If/else if statements

本文关键字:if statements else If C++      更新时间:2023-10-16

我正在编写一个关于克莱默法则和矩阵的程序(空闲时间太多了)。在某个地方,我有这个问题,你需要输入"是"或"否"。如果我输入其他单词(例如lol),它不会转到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++初学者使用codeblocks

仔细阅读do while循环!这是一个post - test循环,所以你可以验证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);