Returning Data from a Struct Array Back to Main

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.

This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  #include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


using namespace 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()
{
	const int 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
	}
}

I wouldn't mess with new right now. You don't really need pointers for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

struct Customer {
	std::string name_first;
	std::string name_last;
};

Customer get_customer() {

	std::string first;
	std::string last;

	std::cout << "Enter the customer's first name: ";
	std::getline(std::cin, first);

	std::cout << "Enter the customer's last name: ";
	std::getline(std::cin, last);

	Customer temp_customer;
	temp_customer.name_first = first;
	temp_customer.name_last = last;

	return temp_customer;
}

int main() {

	const int number_of_customers = 2;
	Customer customers[number_of_customers];

	for (int i = 0; i < number_of_customers; ++i) {
		
		std::cout << "Customer #" << (i + 1) << std::endl;
		
		customers[i] = get_customer();

		std::cout << std::endl;

	}

	std::cin.ignore();
	return 0;
}



EDIT - if you want to have a function populate the entire array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

struct Customer {
	std::string name_first;
	std::string name_last;
};

void populate_customer_array(Customer array[], int size) {

	for (int i = 0; i < size; ++i) {
		std::string first;
		std::string last;

		std::cout << "Customer #" << (i + 1) << std::endl;
		std::cout << "Enter the customer's first name: ";
		std::getline(std::cin, first);

		std::cout << "Enter the customer's last name: ";
		std::getline(std::cin, last);

		array[i].name_first = first;
		array[i].name_last = last;

	}

}

int main() {

	const int number_of_customers = 2;
	Customer customers[number_of_customers];

	populate_customer_array(customers, number_of_customers);

	std::cin.ignore();
	return 0;
}
Last edited on
Thank you so much for your help, xismn! I removed the pointers from my code and now it actually returns my data to the main and other functions. (:
Topic archived. No new replies allowed.