Solved

Lost in program

Posted on 2004-04-04
2
283 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now