简体   繁体   English

如何验证用户输入的月份输入日期?

[英]How to validate user input for month entry in date?

I am writing a program to validate user input for date entry with format 01 Jan 1900. I have written code to validate the leap year and day limit for each month but I have been unable to validate the text entry for each month.我正在编写一个程序来验证用户输入的格式为 1900 年 1 月 1 日的日期输入。我编写了代码来验证每个月的闰年和日限制,但我无法验证每个月的文本输入。

This is the part of the code that is not working yet;这是代码中尚未运行的部分; the validation of the string entry such that any user input apart from Jan, Feb, Mar, Apr.......Dec will throw the error "Wrong entry, please enter valid date"字符串条目的验证,以便除 Jan、Feb、Mar、Apr 之外的任何用户输入.......Dec 将抛出错误“错误的条目,请输入有效日期”

  if (( month != "Jan" || month != "Feb" || month != "Mar" || month != "Apr" 
     || month != "May" || month != "Jun" || month != "Jul" || month != "Aug" 
     || month != "Sep" || month != "Oct" || month != "Nov" || month != "Dec"))    
  {
      std::cout << "Wrong entry, please enter valid date." << std::endl;
  }
  else
  {
      std::cout << day << " " << month << " " << year;
  }

You're checking if the month is not "Jan" OR not "Feb" OR etc. Since the month can't be all possible months at once, your if condition will always be true.您正在检查月份是否不是“一月”或不是“二月”或等等。由于月份不能同时是所有可能的月份,因此您的if条件将始终为真。

Replace the ||替换|| in your condition with && , and it will work correctly.在您的条件下使用&& ,它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM