Solved

Lost in program

Posted on 2004-04-04
2
307 Views
Last Modified: 2010-04-17
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
Comment
Question by:mocca23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
PerryDK earned 125 total points
ID: 10752667
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
 

Author Comment

by:mocca23
ID: 10753129
PerryDK,
Thank you so much for your help. It was very professional. This is what I wanted.

mocca23
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question