Uploaded by vamelat262

Assignment 14

advertisement
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int month;
int day;
int year;
} DATE;
typedef struct {
char name[999];
DATE birthdate;
char gender;
double annualIncome;
} PERSON;
void inputPerson(PERSON* person) {
printf("Enter name: ");
fgets(person->name, 999, stdin);
person->name[strcspn(person->name, "\n")] = '\0';
printf("Enter birthdate (mm/dd/yyyy): ");
scanf("%d/%d/%d", &person->birthdate.month, &person->birthdate.day, &person->birthdate.year);
printf("Enter gender (M,F,O): ");
scanf(" %c", &person->gender);
printf("Enter income: ");
scanf("%lf", &person->annualIncome);
// validate input
if (person->birthdate.month < 1 || person->birthdate.month > 12 || person->birthdate.day < 1 || person->birthdate.day >
31 || person->birthdate.year < 1923 || person->birthdate.year > 2023) {
printf("Invalid date.\n");
inputPerson(person);
return 0;
}
if ((person->birthdate.month == 4 || person->birthdate.month == 6 || person->birthdate.month == 9 || person>birthdate.month == 11) && person->birthdate.day == 31) {
printf("Invalid date.\n");
inputPerson(person);
return 0;
}
if (person->birthdate.month == 2) {
if (person->birthdate.day > 29 || (person->birthdate.day == 29 && (person->birthdate.year % 4 != 0 || (person>birthdate.year % 100 == 0 && person->birthdate.year % 400 != 0)))) {
printf("Invalid date.\n");
inputPerson(person);
return 0;
}
}
if (person->annualIncome < 0 || person->annualIncome > 1000000) {
printf("Invalid income.\n");
inputPerson(person);
return;
}
}
void outputPerson(PERSON* person) {
printf("Name: %s\n", person->name);
printf("Birthdate: %d/%d/%d\n", person->birthdate.month, person->birthdate.day, person->birthdate.year);
printf("Gender: %c\n", person->gender);
printf("Annual Income: $%.2lf\n", person->annualIncome);
printf("\n");
}
int main(void) {
PERSON people[3];
char newline;
for (int i = 0; i < 3; i++) {
printf("Enter details for person %d:\n\n", i + 1);
inputPerson(&people[i]);
while ((newline = getchar()) != '\n' && newline != -1); // clear input buffer
printf("\n");
}
printf("People entered:\n\n");
for (int i = 0; i < 3; i++) {
printf("Person %d:\n", i + 1);
outputPerson(&people[i]);
}
return 0;
}
Download