逻辑或运算符如何在 C++ 的 while 循环中工作?

How does the logic OR operator work inside of a while loop in C++?

我有一个使用逻辑 OR 运算符的 while 循环,但我只能获得关闭循环的条件之一。

while (hourTime <= 23 || input != 4)

我的意思是检查 'hourTime' 变量的条件本身起作用,检查 'input' 的条件也是如此。但是当我使用 OR 运算符组合它们时,只有 'input' 检查有效。我试过将每一个都括在他们自己的括号中,但这似乎也没有解决问题。任何帮助,将不胜感激。如果需要,我可以 post 更多代码。

#define _CRT_SECURE_NO_WARNINGS //added this because I kept getting a warning about using localtime
#include <iostream>
#include <ctime>
#include <string>
#include "stdlib.h"
#include <Windows.h>
#include <iomanip>

using namespace std;


time_t now = time(0); //gets current Time
tm* ltm = localtime(&now); //converts current Time to struct tm type

int hourTime = ltm->tm_hour;
int minuteTime = ltm->tm_min;
int secondTime = ltm->tm_sec;


int input;



//adds another minute or hour when seconds or minutes reach 60
void AddTime() {

    if (secondTime >= 60) {
        minuteTime = minuteTime + 1;
        secondTime = 0;
    }

    if (minuteTime >= 60) {
        hourTime = hourTime + 1;
        minuteTime = 0;
    }
    secondTime = secondTime + 1;

}

//waits a second then clears screen
void WaitAndClear() {

    Sleep(1000);
    cout << "3[2J3[1;1H";

}

//checks user input and handles it accordingly
void CheckUserInput(int userInput) {

    switch (userInput) {
        case 1:
            hourTime = hourTime + 1;
            break;
        case 2:
            minuteTime = minuteTime + 1;
            break;
        case 3:
            secondTime = secondTime + 1;
            break;
        case 4:
            input = 4;
            cout << "You have exited" << endl;
            break;
        default:
            cout << "Invalid Input" << endl;
            break;

    }


}

//displays clock in standard format
void StandardClockTime(int hour, int minute, int second) {

    string amOrPm = "A M";

    if (hour >= 12) {
        amOrPm = "P M";
    }

    if (hour > 12) {
        hour = hour - 12;
    }
    
    cout << "****************************" << endl;
    cout << "*       12-HourClock       *" << endl;
    cout << "*       " << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << " " << amOrPm << "       *" << endl;
    cout << "****************************" << endl;

}

//displays time in military time
void MilitaryTime(int hour, int minute, int second) {

    cout << "****************************" << endl;
    cout << "*       24-HourClock       *" << endl;
    cout << "*         " << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << "         *" << endl;
    cout << "****************************" << endl;
}

//displays menu for user
void DisplayMenu() {

    cout << "****************************" << endl;
    cout << "* 1 - Add One Hour         *" << endl;
    cout << "* 2 - Add One Minute       *" << endl;
    cout << "* 3 - Add One Second       *" << endl;
    cout << "* 4 - Exit Program         *" << endl;
    cout << "****************************" << endl;

}



int main() {

    //these variables are for testing purposes
    hourTime = 22;
    minuteTime = 59;
    secondTime = 50;

    while (hourTime <= 23 || input != 4) {



        WaitAndClear();
        AddTime();
        StandardClockTime(hourTime, minuteTime, secondTime);
        MilitaryTime(hourTime, minuteTime, secondTime); 
        DisplayMenu();
        cin >> input;
        CheckUserInput(input);

        
    }




    return 0;
}

接线员工作正常。如果满足任一条件,则循环继续。如果两个条件都被违反,它就会退出。 (这只是解释 DeMorgan's Theorem)。

如果您希望它在违反任何一个条件时退出,那么您应该使用“逻辑与”(&&) 运算符。

逻辑 OR 运算符仅在其操作数之一的计算结果为真时才计算为真。 如果您想同时检查这两个条件,请使用逻辑 AND 运算符,即 &&。只有在两个条件都为真时才会对其进行评估。您的代码将如下所示。

while (hourTime <= 23 && input != 4){
\Your statement
}