I'm just learning about data structures and how they work. I'm writing a program that has to get a customers information (first name, last name, business address, and home address) and store it in a single struct.
Right now I'm having a hard time understanding how to properly return data from a struct array back to the main function (or any other function for that matter).
Keep in mind that right now I'm not focusing on getting the two addresses from the customer or anything else, but just simply trying to get the data to return from the getCustomer function correctly.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace std;
#define MAX_CUSTOMERS 2
#define SIZE 2
struct Address {
string street; string city; string state;
string zip;
};
struct Customer {
string firstName[SIZE];
string lastName[SIZE];
Address homeAddr[SIZE]; // Nested structure
Address busAddr[SIZE]; // Nested structure
};
//Function Prototypes
int displayMenu ();
Customer getCustomer ();
//Declare an array of type Customer
Customer customerInfo[SIZE];
int main()
{
constint numCust = 0;
int choice; // to hold the users choice
// Call to function showMenu to display the menu to the user
choice = displayMenu();
while (choice !=2) // While user does not quit
{
if (choice == 1)
{
// Call to function getCustomer
getCustomer ();
// Test to see if the array returns to main correctly
cout << "\nTESTING CUSTOMERS NAME: " << customerInfo[0].firstName[0] << " " << customerInfo[0].lastName[0] << "\n\n";
}
else
cout << "Incorrect menu selection, try again" << endl; //input validation
choice = displayMenu();
}
system("pause");
return 0;
}
//***************************************************************************************
// Definition of the function displayMenu which displays a menu to the user and prompts *
// the user to make a choice which then returns the choice to the main. *
//***************************************************************************************
int displayMenu()
{
int choice;
do
{
cout << "Customer Contacts Menu" << endl;
cout << "\nSelect from the menu below" << endl;
cout << "1: Enter new customer" << endl;
cout << "2: Exit program" << endl;
cout << "Please enter your selection: ";
cin >> choice;
return choice;
} while (choice != 2);
}
//*************************************************************************************
// Definition of the fuction getCustomer which asks the user to input the customer’s *
// first name, last name and stores in a single Customer struct *
//*************************************************************************************
Customer getCustomer()
{
Customer* customerInfo = new Customer[MAX_CUSTOMERS];
int count=0;
// -- count set to end when it's < 1 for testing purposes --//
for (count; count < 1; count++){
cout << "\nPlease enter customer's first name: ";
cin.ignore();
getline(cin, customerInfo->firstName[count]);
cout << "Please enter customer's last name: ";
getline(cin, customerInfo->lastName[count]);
cout << "\nThe Customer's name that you entered is: " << customerInfo[0].firstName[count] << " " << customerInfo[0].lastName[count];
cout << endl;
return customerInfo[count]; // I believe this is where I'm having trouble
}
}