Problem with structs and functions

This is a discussion on Problem with structs and functions within the C++ Programming forums, part of the General Programming Boards category; I have to setup a program that sets up a Date Structure (int month, day, year) and a Person structure ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Problem with structs and functions

    I have to setup a program that sets up a Date Structure (int month, day, year) and a Person structure (name and birthday - using the Date struct). Program needs a function (FillPerson) that asks the user to enter all the name and birthday information.

    Here's where I run into trouble.

    Need a function called WritePerson that writes out all the person information using the name of the month ( as opposed to the integer value) for the birthday month.

    I need to have two Person variables and the program should fill these two variables and then call a function called WhoIsOlder, which is sent both person variables. The function determines who is older and writes the age status to the screen. WhoIsOlder should call WritePerson twice.

    I made notes in my code about areas I need help with. Also any suggestions on constructing the WhoIsOlder function are welcome.

    Thank You!

    Code:
     
    #include <iostream.h>
    
    struct Date
    {
    	int month,day,year;
    };
    
    struct Person
    {
    	char name[40];
    	Date birth;
    };
    
    Person FillPerson();
    void WritePerson(Person);
    
    
    int main ()
    {
    
    	char monthlist [12][80] = {"January", "February", "March", "Arpil", "May", "June", "July", "August", "September", 
    								"October", "November", "December"};
    	
    	
    	Person a,b;
    	FillPerson();
    	WritePerson(WHAT GOES HERE?);		//Need help here?
    
    	
    	
    	return 0;
    }
    
    Person FillPerson()			//is this the correct way to enter data for two different variables?
    {
    	Person a,b;
    	
    	cout << "\n Enter the person's name: ";
    	cin >> a.name;
    	
    	cout << "\n Enter the person's birthday in month, day, year, such as 1 21 1965: "<< endl;
    	cin >> a.birth.month >> a.birth.day >> a.birth.year;
    
    	cout << "\n Enter a second person's name: ";
    	cin >> b.name;
    
    	cout << "\n Enter the second person's birthday in month, day, year, such as 1 21 1965: "<< endl;
    	cin >> b.birth.month >> b.birth.day >> b.birth.year;
    
    	return a,b;	
    }
    
    Person WritePerson(Person a, Person b)
    {
    	
    	cout << "\n Person one is " << a.name <<" and was born on " ; 
    	return a, b;												
    }
    
    
    //how do I get the program to write out the month not the integer?
    // using a char array?  how would I do that?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    //how do I get the program to write out the month not the integer?
    // using a char array?  how would I do that?
    you have the names stored in an array so to display the month, it's
    cout<<monthlist[month-1];

    and can you show us what you have so far for the WhoIsOlder function?

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Some things:

    FillPerson function should really ask input for one person only. You can't return two results from a function. (If you really wanted to store info for two persons, you'd have to call FillPerson twice.)
    In main() you should also assign the return value (a Person) to one of the persons (a or b), otherwise the input will simply be lost as soon as the FillPerson function exits.
    Code:
    Person a;
    a = FillPerson();
    WritePerson should have a matching definitions where you implement it
    Code:
     void WritePerson(Person a)
    Again, this function (as the name suggest) should only attempt to print info on one person.

    The idea, that for month you need an array index, is correct. Keep in mind, that arrays are 0-indexed, so you'll have to subtract one:
    Code:
     monthlist[a.birth.month-1]
    WritePerson, because it is a void function, doesn't return anything.
    Because it expects a Person as an argument, in main() you just put the person between the brackets whose data you should get from FillPerson.
    Code:
    WritePerson(a);
    The monthlist array needs to be global (outside any function, i.e outside main), in order to be visible inside WritePerson.

    You should also probably review your materials on functions, because there seems to be a lot of confusion about them (return types, arguments, local and global variables)

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    so would I have to use cout << monthlist[a.birth.month - 1] to properly display the month the user entered for the first person ?

    Also I need help with the call statement for the WritePerson() function. I need to pass the data entered for both persons from the FillPerson function to the WritePerson function. I keep getting errors when trying to complie.

    I haven't started working on the WhoIsOlder function yet as I wanted to have the rest of the program function properly before I started comparing ages.

    Thanks for your help.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    so would I have to use cout << monthlist[a.birth.month - 1] to properly display the month the user entered for the first person ?
    Not in the function WritePerson's context.

    You write one function, but you can call this several times. Each time the argument that you give it shows, which person's birthmonth to display.

    Code:
    //this is the function
    void WriteMonth(Person x)
    {
        cout << monthlist[x.birth.month-1];
    }
    
    //this is how you call it:
    int main()
    {
        Person a, b;
        /*---*/
        WriteMonth(a); //a is passed to WriteMonth, where it is known as x
        WriteMonth(b); //b is passed to WriteMonth, where it is known as x
    }

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Thank you for your help. I will use your suggestions and see what I can come up with.

    Also does anyone have a good reference for a review of functions? I don't like the way my book is laid out for this section and I find it a bit confusing.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Ok I got everything working properly up to the point when I call the function WhoIsOlder. I need to send both person variables to the function and determine who is older and then write the age status to the screen. WhoIsOlder should call WritePerson twice.

    What am I doing wrong? How would I compare the two ages to determine who is older?

    Thank You!



    Code:
    #include <iostream.h>
    
    struct Date
    {
    	int month,day,year;
    };
    
    struct Person
    {
    	char name[40];
    	Date birth;
    };
    
    Person FillPersona();
    Person FillPersonb();
    void WritePersona(Person);
    void WritePersonb(Person);
    Person WhoIsOldera();
    
    
    char monthlist [12][80] = {"January", "February", "March", "Arpil", "May", "June", "July", "August", "September", 
    								"October", "November", "December"};
    
    int main ()
    {
    	
    	Person a,b;
    	a = FillPersona();
    	WritePersona(a);
    	b = FillPersonb();
    	WritePersonb(b);
    	WhoIsOldera();
    
    	
    
    	return 0;
    }
    
    Person FillPersona()
    {
    	Person a;
    	
    	cout << "\n Enter the person's name: ";
    	cin >> a.name;
    	
    	cout << "\n Enter the person's birthday in month, day, year, such as 07 15 1982: "<< endl;
    	cin >> a.birth.month >> a.birth.day >> a.birth.year;
    
    	return a;	
    }
    
    void WritePersona(Person a)
    {
    	cout << "\n The person's name is " << a.name << " and their birthday is " << monthlist[a.birth.month - 1] << " "<< a.birth.day 
    				<<" "<< a.birth.year <<endl;		
    }
    
    Person FillPersonb()
    {
    	Person b;
    	
    	cout << "\n Enter the person's name: ";
    	cin >> b.name;
    	
    	cout << "\n Enter the person's birthday in month, day, year, such as 07 15 1982: "<< endl;
    	cin >> b.birth.month >> b.birth.day >> b.birth.year;
    
    	return b;	
    }
    
    void WritePersonb(Person b)
    {
    	cout << "\n The person's name is " << b.name << " and their birthday is " << monthlist[b.birth.month - 1] << " "<< b.birth.day 
    				<<" "<< b.birth.year <<endl;		
    }
    
    Person WhoIsOldera(Person a)
    {
    	Person age; 
    	Person current_year = 2006;
    	age = current_year - a.birth.year;
    
    	return age;
    }

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    #include <iostream.h>
    This is outdated, use
    Code:
    #include <iostream>
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    http://members.gamedev.net/sicrane/a.../iostream.html

    Code:
    char monthlist [12][80] = {"January", "February", "March", "Arpil", "May", "June", "July", "August", "September", 
    								"October", "November", "December"};
    That is using up space . . . you could use something like this instead.
    Code:
    const char *monthlist[] = {"January", /*...*/ "December"};
    There's something fishy here:
    Code:
    Person WhoIsOldera();
    // ...
        WhoIsOldera();
    // ...
    Person WhoIsOldera(Person a)
    {
    WritePersona() and WritePersonb() are identical.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by dwks
    There's something fishy here:
    Code:
    Person WhoIsOldera();
    // ...
        WhoIsOldera();
    // ...
    Person WhoIsOldera(Person a)
    {
    WritePersona() and WritePersonb() are identical.

    All I essentially need is the FillPerson, WritePerson, and WhoIsOlder functions but I couldn't figure out a way to make them run by using the indiviual data for Person a and Person b, so I created the functions WritePersona() and WritePersonb().

    Any suggestions on how to create it using only the 3 functions?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes.

    A function doesn't care what the variable is called that you pass it. This code works:
    Code:
    void increment(int &variable) {
        variable ++;
    }
    
    int main(void) {
        int x, y, a_really_weird_name;
        x = y = a_really_weird_name = 0;
        increment(x);
        increment(y);
        increment(a_really_weird_name);
    }
    Therefore, you can use the same function for both Person a and b.

    [edit] Like this:
    Code:
    WritePersona(a);
    WritePersonb(b);
    
    void WritePersona(Person a)
    {
    	cout << "\n The person's name is " << a.name << " and their birthday is " << monthlist[a.birth.month - 1] << " "<< a.birth.day 
    				<<" "<< a.birth.year <<endl;		
    }
    
    void WritePersonb(Person b)
    {
    	cout << "\n The person's name is " << b.name << " and their birthday is " << monthlist[b.birth.month - 1] << " "<< b.birth.day 
    				<<" "<< b.birth.year <<endl;		
    }
    ->
    Code:
    WritePerson(a);
    WritePerson(b);
    
    void WritePerson(Person person)
    {
    	cout << "\n The person's name is " << person.name << " and their birthday is " << monthlist[person.birth.month - 1] << " "<< person.birth.day 
    				<<" "<< person.birth.year <<endl;		
    }
    [/edit]

    [edit=2] As for WhoIsOlder(), it sounds like that function ought to take two Person arguments..
    Code:
    Person WhoIsOlder(Person x, Person y) {
        //...
    }
    [/edit]
    Last edited by dwks; 11-30-2006 at 02:34 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Thank You so much for your help. I didn't know that you could create a separate variable name to store information aka person.name , person.month , person.day....etc.

    That's why I had it setup running the same function but with slightly different variable names. I knew there was an easier way.....Thanks for showing it to me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 01:35 AM
  2. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 01:46 PM
  3. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 05:36 AM
  4. Problem displaying (structs functions)
    By dayknight in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2004, 06:11 PM
  5. probs with structs and functions
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-09-2002, 12:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21