Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

c - When checking the validity of date of birth separated by dashes, an error rises when entering characters, and i can't solve that issue

from the requirements of my final project is to validate the scanned date of birth in the following format (DD-MM-YYYY). the date of birth is a struct formed of 3 integers, day, month and year. So, I scan it from the user as follow:

  scanf("%d-%d-%d",&dob.day,&dob.month,&dob.year);

However, when I enter a character, of course it enters an infinite loop. I tried to use the flushinput concept (as I've used it with other scanned integers). It prevents the infinite loop. However, when re-scanning (through a certain loop), it keeps showing the "INVALID EMAIL" message, even though the entered email is correct! I guess this arises due to the presence of the dashes in the input. So, can anybody tell me what to do?

Here's the part of the code where the issue arises (without the flushinput function):

void dobCheck(DATE dob) ///validates date of birth and rescans it if wrong
{
    while(1)
    {
       if (dob.year<1806 || dob.year>2021 || dob.month<1 || dob.month>12 || dob.day<1 || dob.day >31)
       {
          dobCheckAct(dob); continue;
       }
      if ((dob.year%4)==0 && dob.month ==2 && dob.day >29)
      {
          dobCheckAct(dob); continue;
      }
      if (dob.year%4)!=0 && dob.month ==2 && dob.day >28)
      {
          dobCheckAct(dob); continue;
      }
      if (dob.month == 4 ||dob.month == 6 || dob.month == 9 || dob.month == 11) && dob.day> 30)
      {
          dobCheckAct(dob); continue;
      }
      break;
  }  
}
void dobCheckAct(DATE dob) ///action taken in case of invalid date of birth
{
    printf("a
**INVALID DATE OF BIRTH!
");
    scanf("%d-%d-%d",&dob.day,dob.month,dob.year);
}

Thanks in advance!!

question from:https://stackoverflow.com/questions/65886087/when-checking-the-validity-of-date-of-birth-separated-by-dashes-an-error-rises

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...