Question

In: Computer Science

Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...

Struct PERSON Assignment

Outcomes:

Demonstrate the ability to create structs using typedef
Demonstrate the ability to create an array of structs
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.

name [this will be a string]
birthdate [this will be a DATE]
gender [this will be a char]
annualIncome [this will be either float or double, your choice]
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:

Use global variables, in this or any program ever.
Use goto statement(s), in this or any program ever.

Solutions

Expert Solution

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

}


Related Solutions

Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs 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. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs 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. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
Playing with strings Assignment Outcomes: Demonstrate the ability to create strings. Demonstrate the ability to manipulate...
Playing with strings Assignment Outcomes: Demonstrate the ability to create strings. Demonstrate the ability to manipulate strings. Demonstrate the ability to write well written code. Program Specifications: DESIGN and IMPLEMENT a short program that will: Allow the user to enter a string with up to 100 letters. Display the user-entered string: Forward Backward Vertical As a triangle made from the letters of the string Display the number of letters in the string. Once everything above is displayed, the program will...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit: name (string, up to 50 characters long) color (string, up to 10 characters long) fat (integer) sugar (integer) carbohydrate (integer) Write a void function called printFruit that takes a fruitType parameter and prints the data (as shown in the example below). Declare a variable of type fruitType to store the following data: name: banana color: yellow fat: 1 sugar: 15 carbohydrate: 22 then use...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name (size 50) and the salary (double) of an employee. The student_record struct will have the name (size 30), Id number (int) of a student, and the grade (int). Include these two structs in a union called 'person'. Using file redirection called input.txt, at first, ask the user for the employee's information and print them from the main() and the size of the union. Then...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability...
Purpose The purpose of this assignment is to give you an opportunity to demonstrate your ability to identify emerging ethical issues in business, interpret the multitude of perspectives inherent in your case study, and model appropriate behaviour by recommending specific solutions. How to Proceed Select a case. It can be one of the textbook cases that we have not discussed during the course. It can also come from the outside world, perhaps a case you have been following in the...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
C Programming Game Scores Assignment Outcome: Student will demonstrate the ability to design a menu driven...
C Programming Game Scores Assignment Outcome: Student will demonstrate the ability to design a menu driven program. Student will demonstrate the ability to create and use a 2D array on the stack. Student will demonstrate the use of functions. Student will demonstrate good programming style. Program Specifications: *********************************************** ** MAIN MENU ** *********************************************** A) Enter game results B) Current Record (# of wins and # of losses and # of ties) C) Display ALL results from all games WON D)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT