[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

Lost in program

Hi,
 Please, can someone help me with this program? I am totally lost.

This program should have three functions.

Function1 (FillPerson) should ask user to enter all the name and birthday information. Prototype for this function1 is: person FillPerson();

Function2 called WritePerson should write out all the person information using the name of the month (as opposed to the integer value) for the birthday month. The prototype for this function2 is: void WritePerson(Person);

Program should have two Person variables. It should fill these two variables and then call a function called WhoIsOlder. Function3 determines who is older and writes the age status to the screen.
The program is:

#include <iostream.h>

struct Date
{
      int month;
      int day;
      int year;
};

struct Person
{
      char name[40];
      Date birthday;
};

Person FillPerson();
void WritePerson(Person);
int WhoIsOlder(int p1, int p2);
 
int main
{
      Person MyPerson[2];
      Date insert;
      MyPerson = FillPerson();
      return 0;
}

person FillPerson()
{
      
      cout << "\n Enter the person's name:    ";
      cin.getline(MyPerson[0].name,40);
      cout << "\n Enter the person's date of birth:   ";
      cin >> insert[0].month >> insert[0].day >> insert[0].year;
      cin.ignore(10,'\n');
      return (0);
}

void WritePerson(person)
{
      cout << "\n Name of the person is << "MyPersons[0].name << 
      ",and date of birth is "  << insert[0].month << 
      insert[0].day << insert[0].year<< endl;
      cout << "\n The older person is  "<< MyPerson;
      return 0;
}

int WhoIsOlder( int p1, int p2);
{
      if (p1 > p2)  
            {
                  return (p1);
            }
      else (p1 < p2);    
            {
                  return (p2);
            }
}      

Anything would help.      
Thank you,
mocca23
0
mocca23
Asked:
mocca23
1 Solution
 
PerryDKCommented:
first off here is input and output of program

Filling person one

 Enter the person's name:    dan

 Enter the person's date of birth:  month <enter> day <enter> year <enter> 5
15
1961
Filling person two

 Enter the person's name:    bob

 Enter the person's date of birth:  month <enter> day <enter> year <enter> 8
16
1964
Writing person one
Name of the person is dan and date of birth is May 15, 1961
Writing person two
Name of the person is bob and date of birth is August 16, 1964
Person 1 is older


here is the actuall program code.  This should help you get started with your assignment.


#include <iostream.h>

struct Date
{
     int month;
     int day;
     int year;
};

struct Person
{
     char name[40];
     Date birthday;
};

char* ConvertIntMonthToStringMonth(int month)
{
  switch(month)
  {
    case 1:
      return "January";
    break;
    case 2:
      return "Feburary";
    break;
    case 3:
      return "March";
    break;
    case 4:
      return "April";
    break;
    case 5:
      return "May";
    break;
    case 6:
      return "June";
    break;
    case 7:
      return "July";
    break;
    case 8:
      return "August";
    break;
    case 9:
      return "September";
    break;
    case 10:
      return "October";
    break;
    case 11:
      return "November";
    break;
    case 12:
      return "December";
    break;
    default:
      return "Invalid Month";
    break;
  }
}

//FillPerson takes a Person instance passed by reference
//passed by reference is indicated by the &
//and simply means that when we modify the Person instance that it will
//remain modified after the function call has been executed
void FillPerson(Person& person)
{
  cout << "\n Enter the person's name:    ";
  cin >> person.name;
  cout << "\n Enter the person's date of birth:  month <enter> day <enter> year <enter> ";

  cin   >> person.birthday.month
        >> person.birthday.day
        >> person.birthday.year;
  cin.ignore(10,'\n');
}

//WritePerson takes a constant Person reference
//in other words person can not be modifed in this function
void WritePerson(const Person& person)
{
  cout  << "Name of the person is " << person.name
        << " and date of birth is "
        << ConvertIntMonthToStringMonth(person.birthday.month)
        << " "
        << person.birthday.day
        << ", "
        << person.birthday.year
        << endl;
}

//returns 0 if the birth days are equal
//returns 1 if p1 is older than p2
//returns -1 if p2 is older than p1
int WhoIsOlder(const Person& p1, const Person& p2)
{
  if(p1.birthday.year < p2.birthday.year)
    return 1;
  else if(p2.birthday.year < p1.birthday.year)
    return -1;

  //the years must be the same at this point
  if(p1.birthday.month < p2.birthday.month)
    return 1;
  else if(p2.birthday.month < p1.birthday.month)
    return -1;

  //the years and months are the same
  if(p1.birthday.day < p2.birthday.day)
    return 1;
  else if(p2.birthday.day < p1.birthday.day)
    return -1;

  //they are born on the same day
  return 0;
}



int main(int argc, char* argv[])
{
  Person p1, p2;

  cout << "Filling person one" << endl;
  FillPerson(p1);
  cout << "Filling person two" << endl;
  FillPerson(p2);

  cout << "Writing person one" << endl;
  WritePerson(p1);
  cout << "Writing person two" << endl;
  WritePerson(p2);

  int whosOlder = WhoIsOlder(p1, p2);
  if(whosOlder == 1)
  {
    cout << "Person 1 is older" << endl;
  }
  else if(whosOlder == -1)
  {
    cout << "Person 2 is older" << endl;
  }
  else
  {
    cout << "The two people are the same age" << endl;
  }

  return 0;
}
0
 
mocca23Author Commented:
PerryDK,
Thank you so much for your help. It was very professional. This is what I wanted.

mocca23
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now