【发布时间】:2021-09-26 06:45:37
【问题描述】:
我有一个使用逻辑 OR 运算符的 while 循环,但我只能获得一个条件来关闭循环。
while (hourTime <= 23 || input != 4)
我的意思是检查“hourTime”变量的条件本身可以工作,检查“输入”的条件也是如此。但是当我使用 OR 运算符组合它们时,只有“输入”检查有效。我已经尝试将每个人都括在自己的括号中,但这似乎也没有解决问题。任何帮助,将不胜感激。如果需要,我可以发布更多代码。
#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 << "\033[2J\033[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;
}
【问题讨论】:
-
是的,这正是 OR 的意思。它应该检查它们中的任何一个是否为真,如果一个为真,它甚至不需要检查另一个。我猜你想要的是 AND 运算符?
-
当条件不符合预期时,请显示实际值。
-
我了解 OR 运算符的工作原理,但我要说的是,其中一个条件一旦放在一起就完全停止工作,通常是 hourTime。
-
@SpaceyBot 这仅适用于标准运算符||直到 C++17。一旦你重载它,它们就会失去它们特殊的排序属性。
-
代码不够,因为我们不知道您的意见和期望。只需尝试描述程序应在哪些条件下继续运行以及程序应在哪些条件下退出。
标签: c++ loops logical-operators