c++ - while循环的两个条件。我的错误是什么?

标签 c++

我有一个问题。这里的一切似乎都工作正常,除了以下行:

} while (OneMoreTime != 'y' || OneMoreTime != 'n'); 

完整代码;

#include <iostream>
using namespace std;


int main()
{
int ARRAY_LENGTH = 5;
int MyArray[ARRAY_LENGTH] = {1, 2, 3, 4, 5};

cout << "Values in the array: " << ARRAY_LENGTH << endl;

for (char OneMoreTime = '\0'; OneMoreTime = 'n'; )
{

    int WhichNumber = ARRAY_LENGTH;
    do  
    {
        cout << "What numbers from the array do you want to see, counting backwards? ";     
        cin >> WhichNumber;
    } while ((WhichNumber > ARRAY_LENGTH) || (WhichNumber <= 0));


    //calculating the correct position in the array (from start)
    int Number2Print = ARRAY_LENGTH - WhichNumber;


    //printing
    cout << "The number is: " << MyArray[Number2Print] << endl;


    //continue?
    do
    {
        cout << "One more time? (y/n) ";
        cin >> OneMoreTime;
    } while (OneMoreTime != 'y' || OneMoreTime != 'n');

}

return 0;
}

我得到的是它在第一次成功打印后不断询问“再来一次?(y/n)”。如果我只使用一个条件,它将起作用(但这还不够)。

最佳答案

该条件将始终为真,因为 OneMoreTime 不能同时等于 ny。您可能的意思是使用 && (和)

while (OneMoreTime != 'y' && OneMoreTime != 'n'); 

关于c++ - while循环的两个条件。我的错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27612628/

相关文章:

C++ MFC SDI 创建子窗口

c++ - 为什么我们不直接使用 Object 文件?

c++ - 在 Mathematica 中绘制来自 c++ 的二维点阵数据

c++ - 如何使用 c-ares 将 IP 解析为主机?

c++ - C++ 如何实现 std::is_class

c++ - 解析通过 netcat 发送的字节

c++ - 代码不工作。使用堆栈

c++ - 巨大的初始化列表,如何修复 "fatal error C1060: compiler is out of heap space"

c++ - 运算符重载的基本规则和惯用法是什么?

c# - Windows Mobile 平台上的 PInvoke