Write a program that uses a structure to store the following data about a custom
ID: 3530004 • Letter: W
Question
Write a program that uses a structure to store the following data about a customer account: Name Address City, state, and ZIP Telephone number Account Balance Date of last payment The program should use an vector of at least 20 structures. It should let the user enter data into the vector, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Input Validation: When the data for a new account is entered, be sure the user enters data for all the fields. No negative account balances should be entered.Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct customer{ //customer structure datatype
string name, address, city, state, zip, telephone, date;
int balance;
};
void setCustomer(customer&); //inputs customer data from user
void getCustomer(customer); //prints customer data to the screen
int main()
{
int size,action=1;
cout<<"-+- Customer Manager -+- How many customers?: ";
cin>>size;
customer*database=new customer[size];
cout<<" --- Created database of "<<size<<" customer(s) --- ";
while(action!=0){ //Menu Interface Loop
cout<<"-+- Menu -+- 0: Quit 1: Set a customer's information 2: Display a customer's information >";
cin>>action;
cout<<endl;
switch(action){
case 1:{
int cnum;
cout<<"customers: ";
for(int x=0;x<size;x++)
cout<<x+1<<" : "<<database[x].name<<endl;
cout<<" customer number to change: ";
cin>>cnum;
if(cnum<=size && cnum>0)
setCustomer(database[cnum-1]);
else
cout<<" invalid customer number! ";
}
break;
case 2:{
int cnum;
cout<<"customers: ";
for(int x=0;x<size;x++)
cout<<x+1<<" : "<<database[x].name<<endl;
cout<<" customer number to display: ";
cin>>cnum;
if(cnum<=size && cnum>0)
getCustomer(database[cnum-1]);
else
cout<<" invalid customer number! ";
}
break;
default: break;
}
}
delete[] database;
return 0;
}
void setCustomer(customer&set)
{
cout<<"Enter Data for the Customer: Name: ";
cin>>set.name;
cout<<"Address: ";
cin.ignore(256,' ');
getline(cin, set.address);
cout<<"City: ";
cin>>set.city;
cout<<"State: ";
cin>>set.state;
cout<<"Zip Code: ";
cin>>set.zip;
cout<<"Telephone Number: ";
cin>>set.telephone;
cout<<"Date of last Payment: ";
cin>>set.date;
do{
cout<<"Account Balance: ";
cin>>set.balance;
if(set.balance<0)
cout<<"Invalid Balance! ";
}while(set.balance<0); //data validation
}
void getCustomer(customer get)
{
cout<<" --- Data for "<<get.name<<" --- ";
cout<<" Address: "<<get.address<<", "<<get.city<<", "<<get.state<<", "<<get.zip;
cout<<" Telephone: "<<get.telephone;
cout<<" Last Payment: "<<get.date;
cout<<" Account balance: "<<get.balance<<endl<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.