In: Computer Science
Struct PERSON Assignment
Outcomes:
Program Specifications:
DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON.
Create a struct using the typedef command for a DATE.
Create a struct for a PERSON with the following fields.
Create three variables of type PERSON. Create a function that populates the data for each person (all 3 of them). Create a function that outputs all data about each of the people in a nice formatted manner.
Data Validation:
All dates entered must be validated. Make sure you account for the number of days in each month, the fact that there are exactly 12 months and every four years there is a leap year.
The name for each PERSON will be stored in sentence case.
The gender will either be M, F, or O.
The annual income will between 0 dollars and 1 million dollars.
Submission Requirements:
Requirements will be same as the first assignment which will be the same for all future assignments except that you do NOT NEED a design tool for this assignment.
DO NOT:
NOTE: I ALREADY SUBMITTED THIS QUESTION PREVIOUSLY
BUT FOR YOU I AGAIN SUBMITTING THIS QUESTION WITH SOME CHANGES ...
PREVIOUSLY I SUBMITTED THIS QUESTION IN C LANGUAGE NOW I SUBMITTED THIS IN C++ ...
typedef is a reserved keyword in both c and c++ , it assigns a alteranative names to the existing types
the array of structures is also known as the collection of structures,it is used to store multiple entities of different types
CODE:
OUTPUT:
Raw_code:
//includind iostream for input and ouput streams
#include<iostream>
//including cstring functions
#include<cstring>
//using standard namespacing
using namespace std;
//FUNCTION PROTOTYPES
//i implemented new functions for the code readability
//I IMPLENTED A NEW FUNCTIONS FOR CODE READABILITY
//leap year function to check whether the given year in the date of
birth is leap year or not
int leapyear(int);
//check_date function prototype to check the whether the given date
is valid or not
int check_date(int ,int ,int);
//valid_gender function prototype to check whether the given gender
is valid or not
int valid_gender(char c);
//sentence_case function prototype to check whether the given name
in sentence case or not
int sentence_Case(char *);
//typedef is used to give new name for the existing name in c
program
//DATE structure with 3 members for day ,month and year
typedef struct
{
//integer variable for date
int dd;
//integer variable for month
int mm;
//integer variable for year
int yyyy;
}DATE;//structure name DATE
//PERSON structure 4 members for name,date of birth that is another
structure , gender and annual income
typedef struct
{
//character array for name
char name[30];
//structure variable of date of birth this is best
advantage of typedef we do not need to always specify the
//struct structname structure_member just we need
structure_name structure_member
DATE birthdate;
char gender[1];
double annualIncome;
}PERSON;//structure name PERSON
//function to get the details of the person
PERSON setData_of_Person()
{
//PERSON structure member
PERSON ob;
int valid;
//enter the name of the person
cout<<"\nEnter the Name:";
cin>>ob.name;
//if the name is not in sentence case this loop
executed
while(sentence_Case(ob.name)!=1)
{
cout<<"\nInvalid name!\nEnter
the name again [ex:Tony ,Robert]:";
cin>>ob.name;
}
//enter the date of birth of the person
cout<<"\nEnter the date of birth (dd mm
yyyy):";
cin>>ob.birthdate.dd>>ob.birthdate.mm>>ob.birthdate.yyyy;
valid=check_date(ob.birthdate.dd,ob.birthdate.mm,ob.birthdate.yyyy);
//if the date is not valid this loop is executed
while(valid!=1){
cout<<"\nInvalid date:\nEnter
the date of birth again:";
cin>>ob.birthdate.dd>>ob.birthdate.mm>>ob.birthdate.yyyy;
valid=check_date(ob.birthdate.dd,ob.birthdate.mm,ob.birthdate.yyyy);
}
//enter the gender of the person
cout<<"\nEnter the gender (M or F or O):";
cin>>ob.gender;
//if the gender is not valid this loop is
executed
while(valid_gender(ob.gender[0]))
{
cout<<"\nInvalid gender
\nEnter the gender again:";
cin>>ob.gender;
}
//enter the annnual income of the person
cout<<"\nEnter the annualIncome:";
cin>>ob.annualIncome;
//if the annual income is not valid this loop is
executed
while(ob.annualIncome<0||ob.annualIncome>100000)
{
cout<<"\nEnter valid
annualIncome:";
cin>>ob.annualIncome;
}
return ob;
}
//function to display the function
void getData_of_Person(PERSON ob)
{
cout<<"\nName:"<<ob.name;
cout<<"\nDate of
birth:"<<ob.birthdate.dd<<"-"<<ob.birthdate.mm<<"-"<<ob.birthdate.yyyy;
cout<<"\nGender:"<<ob.gender;
cout<<"\nAnnual
Income:$"<<ob.annualIncome;
cout<<"\n------------------------------------------";
}
//main function
int main()
{
int number_of_people,i;
//taking the value of number of people
cout"Enter the number of people:";
cin>>number_of_people;
//array of structures
PERSON ob[number_of_people];
//taking the information about the people
for(i=0;i<number_of_people;i++)
{
ob[i]=setData_of_Person();
}
cout<<"\nThe Details are:";
//displaying the people information
for(i=0;i<number_of_people;i++)
{
getData_of_Person(ob[i]);
}
return 0;
}
//fucntion definition to check the given name is in sentence_case
or not
int sentence_Case(char *name)
{
int i=1;
//checking the first letter of the name is capital or
not
if(name[0]>=65&&name[0]<=90)
{
//checking remaining letters are
small case
while(name[i]>=97&&name[i]<=122)
i++;
//at the end the stringlenth is
equal to the i variable we conculde that given name is valid
//then return 1
if(i==strlen(name))
return 1;
//else return 0
else
return 0;
}
//if the first letter of the name is not capital it is
not in sentence case then return 0
else
return 0;
}
//function defintion to check the given the gender is valid or
not
int valid_gender(char c)
{
//if the gender M or F or O return 0
if(c=='m'||c=='M'||c=='F'||c=='f'||c=='O'||c=='o')
return 0;
//else return 1;
else
return 1;
}
//function defintion to check the date of birth is valid or
not
int check_date(int d,int m,int y)
{
//if the days are less than or equal to 31 ,months are
less than or equal to 12 and year is greater than 0
//this if condition is executed
if(d<=31&&m<=12&&y>0)
{
//if the days
are valid according to their months
if(d<=31&&(m==1||(m==3)||m==5||m==7||m==8||m==10||m==12))
{
return 1;
}
else
if(d<=30&&(m==4||m==6||m==9||m==11))
{
return 1;
}
//if the day is
29 , month is 2 and the year is leap year it is valid date
else
if(d==29&&m==2&&leapyear(y))//function calling to
check the year is valid or not
{
return 1;
}
else return
0;
}
else
return 0;
}
//function defintion to check the year is leap year or not
int leapyear(int year)
{
//if the year is divisible by 4 only it is a leap
year
//if the year is divisible by 4 ,100 and 400 it is a
leap year
//in all other conditions it is not a leap year
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}
*************************For any queries comment me in the comment box*************************