I have created two functions one to test if a year is a leap year and another to test for a valid day and month and the code works perfectly. But im having a problem if the date is wrong I want the user to keep entering data until they enter the correct date and if they enter the correct date I want the information stored in the struct. Currently the while loop keeps looping if the date is correct or wrong. The if statement (if (CAPACITY < count)) allows a user to enter one item at a time. Can someone help?
Code:#include<stdio.h> #include<ctype.h> #include<stdlib.h> #define bool int #define YES 1 #define NO 0 #define CAPACITY 20 #define SIZE 50 #define TRUE 1 #define FALSE 0 typedef struct { int day; int month; int year; }Date; typedef struct { char name[SIZE]; Date dob; }Student; void checkDate(Student data[], int count); bool isLeapYear(int year); int main (void) { bool check = FALSE; int i, count = 0; Student database[CAPACITY]; if (CAPACITY > count) { while(check == FALSE) { printf("Please enter a person's first name, lastname and date of birth\n"); scanf("%s %s %d %d %d", database[count].name, &database[count].dob.day, &database[count].dob.month, &database[count].dob.year); checkDate(database, count); }/*end while(check == FALSE) */ count++; }/*end if (CAPACITY > count) */ else { printf("The database is full\n"); } return 0; }/*end main() function */ /***************************************************************************/ /*Function to check valid date */ /***************************************************************************/ void checkDate(Student data[], int count) { int monthLength[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; bool check = FALSE; /*Add 29 days to Feburary if year is a leap year*/ if (isLeapYear(data[count].dob.year)) { monthLength[2] = 29; } /*If month entered is less then one and greater than 12 print error message*/ if (data[count].dob.month < 1 || data[count].dob.month > 12) { printf("Invalid month\n"); check = FALSE; } /*If day entered is less then 1 and greater then month number in the array month_length print error message*/ else if (data[count].dob.day < 1 || data[count].dob.day > monthLength[data[count].dob.month]) { printf("Invalid day\n"); check = FALSE; } }/* end bool checkDate(Student data[], int count) */ /***************************************************************************/ /*Function to check if a year is a leap year */ /***************************************************************************/ bool isLeapYear(int year) { bool valid; if ((year%4) !=0) { valid = NO; } else if ((year%400) == 0) { valid = YES; } else if ((year%100) == 0) { valid = NO; } else { valid = YES; } return (valid); }/*end bool isLeapYear(int year) */



LinkBack URL
About LinkBacks



and thank you everyone else for your help and advice also