C++ 输入时间时,我如何划分界限?
我现在正在输入一个程序,它需要我输入出发和到达时间。如何使输入值必须在0:00和23:59之间 现在我有C++ 输入时间时,我如何划分界限?,c++,validation,time,C++,Validation,Time,我现在正在输入一个程序,它需要我输入出发和到达时间。如何使输入值必须在0:00和23:59之间 现在我有 cout << "\n\nEnter the departure time on the first day of the trip : "; cin >> departure_Time; cout << "\n\nEnter the arrival time on the last day of the trip : "; cin >> a
cout << "\n\nEnter the departure time on the first day of the trip : ";
cin >> departure_Time;
cout << "\n\nEnter the arrival time on the last day of the trip : ";
cin >> arrival_Time;
cout>出发时间;
到达时间;
我想让它们不会超出界限。尝试使用
while
循环测试出发时间和到达时间是否在界限之内。然后根据结果,移动到程序的其余部分,或者将用户重定向回起始位置
这可以通过多种方式实现。在不知道您计划如何使用数据的情况下,仅基于您提供的代码,此示例应说明while
循环检查无效输入的功能
int main()
{
beginning:
double departure_Time = 0.00, arrival_Time = 0.00;
cout<<"\n\nEnter the departure time on the first day of the trip : ";
cin>> departure_Time;
cout<<"\n\nEnter the arrival time on the last day of the trip : ";
cin>> arrival_Time;
while (departure_Time < 0.00 || arrival_Time > 23.59) {
cout<<"\n\nTry again...";
cin.clear();
cin.sync();
goto beginning;
}
return 0;
}
intmain()
{
开始:
双程出发时间=0.00,到达时间=0.00;
不能按时出发;
不能按时到达;
同时(出发时间<0.00 |到达时间>23.59){
可能你现在没有太多!你尝试过什么?什么类型的是出发时间/到达时间…对不起!我刚刚复制了与我需要帮助的部分有关的部分。它们是双倍的。你如何输入?比如12.00?但是,将小时和分钟分开,检查值是否在范围内(0-23,0-59)。