In: Computer Science
Comments has been given inside the code at appropriate places , wherever required.
# include<bits/stdc++.h>
using namespace std;
// define struct to maintain max and min valid years range
struct valid_years {
static const int MAX_VALID_YR = 9999;
static const int MIN_VALID_YR = 1800;
};
// struct for date and typedef to call it via DATE name
typedef struct Date {
int date, month, year;} DATE;
// struct for holding person details
typedef struct Person_details {
string name;
DATE birthday;
char gender;
float annual_income;
} PERSON;
// check whether year is leap or not to validate the date of Feb month
bool isLeap(int year) {
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
bool isValidDate(int d, int m, int y)
{
if (y > valid_years::MAX_VALID_YR || y < valid_years::MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 || m == 9 || m == 11)
return (d <= 30);
return true;
}
/*
check recursively whether gender is correct or not
*/
char check_gender(char gender) {
if(gender=='M' || gender=='F' || gender=='O')
return gender;
cout<<"\n Entered gender is not correct.... Enter again(M,F,O)\n";
char re_gender;
cin>>re_gender;
return check_gender(re_gender);
}
/*
check recursively whether income lies in the range or not
*/
float check_income(float income) {
if(income>=0.00 && income<= 1000000.00 )
return income;
cout<<"\n Entered income is not in range (0 - 1000000).... re enter. \n";
float re_income;
cin>> re_income;
return check_income(re_income);
}
/*
check recursively whether date is correct or not
*/
DATE check_date(DATE birthday) {
if(isValidDate(birthday.date, birthday.month, birthday.year))
return birthday;
cout<<"\n entered birthday is not valid... enter again\n";
DATE d1;
cin>>d1.date>>d1.month>>d1.year;
return check_date(d1);
}
/*
converts first letter of name to uppercase
*/
char check_name(char name) {
return toupper(name);
}
/*
this method validate the entered data for each person
*/
void validate(PERSON person[],int size) {
for(int i=0;i<size;i++) {
cout<<"\n ....verifying person: "<<i+1;
person[i].name[0] = check_name(person[i].name[0]);
person[i].gender = check_gender(person[i].gender);
person[i].annual_income = check_income(person[i].annual_income);
person[i].birthday = check_date(person[i].birthday);
}
}
/*
this method populate the Person Object for each person
*/
void populate_person(PERSON person[],int size) {
for(int i=0;i<size;i++) {
cout<<"\n----------------------------------";
cout<<"\n\nEnter details for person: "<<i+1<<"\n";
cout<<"\n enter name\n";
cin>>person[i].name;
cout<<"\n enter birthday is (dd mm yyyy) format\n";
cin>>person[i].birthday.date>>person[i].birthday.month>>person[i].birthday.year;
cout<<"\n enter gender:\n";
cin>>person[i].gender;
cout<<"\n enter annual income:\n";
cin>>person[i].annual_income;
cout<<"\n----------------------------------";
}
}
/*
this method display the Person object for each person
*/
void show_person(PERSON person[],int size) {
for(int i=0;i<size;i++) {
cout<<"\n ****** Details for person:"<<i+1;
cout<<"\n name: "<<person[i].name;
cout<<"\n birthday: "<<person[i].birthday.date<<"-";
cout<<person[i].birthday.month<<"-"<<person[i].birthday.year;
cout<<"\n Gender: "<<person[i].gender;
cout<<"\n annual income: "<<person[i].annual_income<<"\n\n";
}
}
int main() {
PERSON person[3];
populate_person(person,3); // enter details for person
validate(person,3); // validate each person object
show_person(person,3); // show person details
}