Using Microsoft Visual Studio 2015 or 2017 C++, write a program that uses an arr
ID: 3904024 • Letter: U
Question
Using Microsoft Visual Studio 2015 or 2017 C++, write a program that uses an array of nested structs to store the addresses for your store’s customers. • Each customer has a name and two addresses: home address and business address. • Each address has a street, city, state, and zip code. 1. Data structure a. Define an Address struct with street, city, state and zip fields b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields of type Address c. Declare an array of type Customer 2. Use a menu?driven program with the following selections: a. Enter new customer b. Display all customers c. Display a particular customer d. Exit the program 3. Define the following 5 functions a. int displayMenu(); Outputs the menu selections Inputs the users selection Validates that the user has entered a valid selection b. Customer getCustomer(); Asks the user to input the customer’s first name, last name and the two addresses and stores in a single Customer struct c. void showCustomer(Customer); Outputs the information for a single Customer struct d. Address getAddress(); Asks the user to enter each component of the address (street, city, state and zip) and stores in a single address struct Note that street will contain embedded blanks, so you will need to use getline. Since you are mixing cin and getline, you will need to use cin.ignore to skip over the last endline character in the input prior to using getline. e. void findCust(Customer[], int); Asks the user to enter a customer’s first and last names Searches the array of Customers for a match If there is a match, prints out all information for the particular customer If not match, prints an error message.
Explanation / Answer
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Address {
string street;
string city;
string state;
int zip;
};
struct Customer {
string lastNm;
string firstNm;
Address homeAddr;
Address busAddr;
};
int displayMenu() {
cout << "1. Enter new customer" << endl;
cout << "2. Display all customers" << endl;
cout << "3. Display a particular customer" << endl;
cout << "4. Exit the program" << endl;
cout << "Enter a command: ";
int cmd;
cin >> cmd;
if (cmd >= 1 && cmd << 4)
return cmd;
else {
cout << "Invalid input, try again..";
return displayMenu();
}
}
Address getAddress() {
Address addr;
cin.ignore();
cout << "Enter address: ";
getline(cin, addr.street);
cout << endl << "Enter city: ";
cin >> addr.city;
cout << endl << "Enter state: ";
cin >> addr.state;
cout << endl << "Enter zip: " << endl;
cin >> addr.zip;
return addr;
}
Customer getCustomer() {
Customer cust;
cout << "Enter last name: ";
cin >> cust.lastNm;
cout << endl << "Enter first name: ";
cin >> cust.firstNm;
cout << endl << "Enter home address:" << endl;
cust.homeAddr = getAddress();
cout << endl << "Enter business address:" << endl;
cust.busAddr = getAddress();
}
void showCustomer(const Customer& customer) {
cout << "Last name: " << customer.lastNm << endl;
cout << "First name: " << customer.firstNm << endl;
cout << "Home Address: " << customer.homeAddr.street << ", " << customer.homeAddr.city << ", " << customer.homeAddr.state << "-" << customer.homeAddr.zip << endl;
cout << "Business Address: " << customer.busAddr.street << ", " << customer.busAddr.city << ", " << customer.busAddr.state << "-" << customer.busAddr.zip << endl;
}
void findCust(const vector<Customer>& customers) {
string fnm, lnm;
cout << "Enter first name: ";
cin >> fnm;
cout << "Enter last name: ";
cin >> lnm;
for (const Customer c : customers) {
if (c.lastNm == lnm && c.firstNm == fnm)
{
showCustomer(c);
return;
}
}
cout << "Customer not found!";
return;
}
int main()
{
vector<Customer> customers;
while (1) {
int cmd = displayMenu();
if (cmd == 4)
exit(0);
switch (cmd)
{
case 1:
customers.emplace_back(getCustomer());
break;
case 2:
for (const Customer c : customers) {
showCustomer(c);
}
break;
case 3:
findCust(customers);
break;
}
}
return 0;
}