简体   繁体   中英

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.

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"

  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.

Replace the || in your condition with && , and it will work correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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