Thread: Passing structures between functions

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Question Passing structures between functions

    I am new to C++, so please explain why what you submit works.

    I am trying to pass a structure between functions. I have 3 files, one with a small main function. The only thing it does is call my menu:

    #include "pim.hpp"

    main()
    {
    menu_choice();
    return 0;
    }

    I have a header file which defines everything:

    #include <iostream>
    using namespace std;

    /* CLS Added to clear screen after showing calculations */
    #define CLS system("cls")

    const int LAST_NAME = 26;
    const int FIRST_NAME = 16;
    const int MI = 2;
    const int ADDRESS = 51;
    const int CITY = 31;
    const int STATE = 26;
    const int PHONE = 13;

    struct contact
    {
    char lname[LAST_NAME];
    char fname[FIRST_NAME];
    char mi[MI];
    char address[ADDRESS];
    char city[CITY];
    char state[STATE];
    int zip;
    char phone[PHONE];
    int birthYear;
    }temp;


    // Define Prototypes
    void print_menu();
    void menu_choice(void);
    void add_contact();
    void print_contact();

    This is the main part of my program:

    #include "pim.hpp"



    // Prints the main menu.

    void print_menu()
    {
    CLS;

    // Print Menu.

    << "Personal Information Manager (PIM) Program " << endl
    << endl << endl
    << "(1) Add New Contact" << endl
    << "(2) Print Contact Information" << endl
    << "(3) Exit" << endl << endl << endl;
    }

    // Menu Option Function:

    void menu_choice()
    {
    int choice;

    while (1)
    {

    print_menu();

    cout << "Select the operation to perform: " << endl;
    cin >> choice;

    while ((choice <1) || (choice > 3))
    {
    CLS;

    cout << endl << "Invalid Choice!" << endl
    << "Please Select (1) to Add New Contact"
    << endl
    << "Please Select (2) to Print Contact Information"
    << endl
    << "Please Select (3) to EXIT program." << endl

    << endl << endl << "Please Re-enter your choice: ";
    cin >> choice;
    }


    switch (choice)
    {
    case 1: add_contact();
    break;

    case 2: print_contact();
    break;

    case 3: exit(EXIT_SUCCESS);
    break;
    }
    }
    }
    // Add_contact Function
    void add_contact()
    {

    contact * c = new contact;

    CLS;

    cin.get();

    cout << "Enter Last Name: ";
    cin.getline(c->lname, LAST_NAME);
    cout << endl;

    cout << "Enter First Name: ";
    cin.getline(c->fname, FIRST_NAME);
    cout << endl;

    cout << "Enter MI: ";
    cin.getline(c->mi, MI);
    cout << endl;

    cout << "Enter Street Address: ";
    cin.getline(c->address, ADDRESS);
    cout << endl;

    cout << "Enter City: ";
    cin.getline(c->city, CITY);
    cout << endl;

    cout << "Enter State: ";
    cin.getline(c->state, STATE);
    cout << endl;

    cout << "Enter ZIP: ";
    (cin >> c->zip).get();
    cout << endl;

    cout << "Enter Phone Number (Including Hyphens): " << endl
    << "Example: 123-45-6789 ";
    cin.getline(c->phone, PHONE);
    cout << endl;

    cout << "Enter Year of Birth: ";
    (cin >> c->birthYear).get();
    cout << endl;
    }

    // Print Contact Function

    void print_contact()
    {

    CLS;

    cout << "Name Address Phone Born" << endl
    << "___________________________________" << endl;

    cout.width(25);
    cout.setf(ios_base::left, ios_base::adjustfield);
    cout << c->name; //error line
    }

    I get 2 errors when I run this program, and both are due to the fact that I cannot determine how to pass the structure between the add_contact function and the print_contact funtcion. The 2 errors are:

    error C2065: 'c' : undeclared identifier

    error C2227: left of '->name' must point to class/struct/union

    I don't expect anyone to do my work for me, however I am completely stuct here. Please help,
    Thanks,
    Alan
    I am using visual C++ 6.0 if that helps.
    I am sorry the code does not seem to line up. But hopefully it is enough to describe the problem.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    use VB code to make your code line up properly
    Code:
    struct FOO  {
        blah, blah
    };
    
    void do_stuff(FOO *foo)  // '*' tells the compiler is is receiving a memory address instead of the actual function
    {
        blah blah
    }
    
    void do_more_stuff(FOO *foo)
    {
        blah blah
    }
    
    main()
    {
        FOO foo;
        do_stuff(&foo);  // '&' is the address of operator
        do_more_stuff(&foo);
    
        return 0;
    }

  3. #3
    Unregistered
    Guest
    Either make the new contact a global variable (not my prefered approach), pass a given struct back and forth between functions(doable if you are only going to use a single struct at a time), or pass a container that can hold a group of contacts back and forth (my preference). Depending on your preference the container could be an array, or a list, or a stack, or whatever. I will use a dynamically allocated array.

    struct contact
    .
    .
    .
    void add_contact(contact *);

    int main(){
    contact array = new contact[100];
    menu_options();
    delete [] array;
    return 0;
    }

    void add_contact(contact * array)
    {
    int i = sizeof array/sizeof array[0];
    cout << "enter id number" << endl;
    cin >> array[i - 1].id;
    .
    .
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  4. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 02:17 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM