In: Computer Science
Language: c++
works in visual basic
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. Requirements: 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 it 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.
#include<bits/stdc++.h>
using namespace std;
#define Size 100000
// Defined Structure Address
struct Address {
string street, city, state, zipcode;
};
// Defined Structure Customer
struct Customer {
string firstNm, lastNm;
Address homeAddr, busAddr;
};
// Function to display menu
int displayMenu() {
int choice;
cout<<endl;
cout<<"--------------MENU--------------"<<endl;
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<<endl<<"Enter your choice : ";
cin>>choice;
return choice;
}
// Function to get address input from user
Address getAddress() {
string street, city, state, zipcode;
Address addr;
cout<<"---- Enter Address ----"<<endl;
cout<<"Enter street : ";
getline(cin,street);
addr.street = street;
cout<<"Enter City : ";
cin>>city;
addr.city = city;
cout<<"Enter State : ";
cin>>state;
addr.state = state;
cout<<"Enter Zipcode : ";
cin>>zipcode;
addr.zipcode = zipcode;
return addr;
}
// Function to get customer details input from user
Customer getCustomer() {
Address homeAddr, busAddr;
string firstNm, lastNm;
Customer cus;
cout<<"------ Enter New Customer ------"<<endl;
cout<<"Enter First Name : ";
cin>>firstNm;
cus.firstNm = firstNm;
cout<<"Enter Last Name : ";
cin>>lastNm;
cus.lastNm = lastNm;
cout<<"Enter Details for Home Address"<<endl;
homeAddr = getAddress();
cus.homeAddr = homeAddr;
cout<<"Enter Details for Business Address"<<endl;
busAddr = getAddress();
cus.busAddr = busAddr;
return cus;
}
// Function to show Address
void showAddress(Address addr) {
cout<<"Street : "<<addr.street<<endl;
cout<<"City : "<<addr.city<<endl;
cout<<"State : "<<addr.state<<endl;
cout<<"Zipcode : "<<addr.zipcode<<endl;
}
// Function to show Customer
void showCustomer(Customer cus) {
cout<<"First Name : "<<cus.firstNm<<endl;
cout<<"Last Name : "<<cus.lastNm<<endl;
cout<<"Home Address Details"<<endl;
showAddress(cus.homeAddr);
cout<<"Business Address Details"<<endl;
showAddress(cus.busAddr);
cout<<"--------------------------------------"<<endl;
}
// Function to find a particular customer
void findCust(Customer cus[], int n) {
string firstNm, lastNm;
cout<<"---- FINDING CUSTOMER ----"<<endl;
cout<<"First Name : ";
cin>>firstNm;
cout<<"Last Name :";
cin>>lastNm;
int flag = 0;
for(int i = 0 ; i < n ; i++) {
if(cus[i].firstNm == firstNm && cus[i].lastNm == lastNm) {
flag++;
cout<<"Customer Found ..!!"<<endl;
showCustomer(cus[i]);
break;
}
}
if(flag == 0) cout<<"Error .. !! Customer Not Found."<<endl;
}
int main() {
int choice, n = 0;
Customer cus[Size];
while(1) {
choice = displayMenu();
if(choice == 1) {
Customer newCus = getCustomer();
cus[n++] = newCus;
} else if(choice == 2) {
for(int i = 0 ; i < n ; i++) {
cout<<"Customer No. "<<i<<endl;
showCustomer(cus[i]);
}
} else if(choice == 3) {
findCust(cus,n);
} else {
break;
}
}
return 0;
}