Solved

Lost in program

Posted on 2004-04-04
2
271 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
Comment Utility
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
Comment Utility
PerryDK,
Thank you so much for your help. It was very professional. This is what I wanted.

mocca23
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
This is an explanation of a simple data model to help parse a JSON feed
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

7 Experts available now in Live!

Get 1:1 Help Now