在c++中设置最大值

Setting a max number in c++

本文关键字:最大值 设置 c++      更新时间:2023-10-16

可能是一个简单的答案,但这已经困扰了我一段时间了。我似乎在设置最大值时遇到了问题。例如,我向用户询问本月使用的小时数,最大小时数不能超过744。我该如何设置这个最大值?提前感谢您的帮助。

使用if语句:

if (hours < max_hours) {
  // Do stuff
}
else {
  std::cout << "Invalid Hours";
}
int main() {
int workhours;
std::cout << "Please enter your workhours: ";
std::cin >> workhours;
if (workhours < max_hours) {
    //good, do your stuff
else {
    std::cout << "Invalid Working Hours";
    std::cout << "Please enter your real workhours: ";
    std::cin >> workhours;
     }}

}