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.