In: Computer Science
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 array of at least 5 structures. It should
let the user enter data into the array, change the contents of any
element and display all the data stored in the array. The program
should have a menu-driven interface and use functions as
appropriate.
Input validation: when the data for a new account is entered, be
sure the user enters data for all the fields (no empty fields). No
negative account balances should be entered.write in c++
The code has been kept as simple as possible, also in the case of displaying the sorted data, the sorting attribute and order were not mentioned so it is been assumed sorting by name and in increasing order. The functions have been defined for all the operations in the structure array. Explanation of important code also has been provided with upper case letters in the comment lines. Also a screenshot of sample running code have been added below the code, i have checked it well but still If you have any query regarding the solution or you didn't get what you wanted or want any modification you can just mention it in the comment section, I will reach you as soon as possible.
CODE:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef struct Customer{ // MAKING THE STRUCTURE OF ALL ATTRIBUTES MENTIONED
string name,address,city,state,dateOfLastPayment;
int zip,telephoneNumber,accountBalance;
}Customer;
int take_int_input(){ // FOR TAKING A VALID INTEGER INPUT
int x;
string s;
while(1){
getline(cin,s); // TOOK THE INPUT IN AN STRING, CHECK IF IT IS CONVERTIBLE TO INTEGER THEN CONVERT AND STORE ELSE SHOW ERROR
try{
x = stoi(s); // STOI CONVERTS STRING TO INTEGER
}
catch (const std::exception& ex) {
std::cerr << "Invalid input!,enter again.. " << endl;
continue;
}
if (x>=0) // IF VALUE IS POSITIVE, IT IS ACCEPTABLE ELSE SHOW IT CAN'T BE NEGATIVE AND ASK TO ENTER AGAIN
break;
else
cout<<"It can't be negative, enter again!!"<<endl;
}
return x;
}
string take_string_input(){ // SIMILARLY FUNCTION FOR TAKING A VALID STRING INPUT
string s;
while(1){
getline(cin,s);
if (s.length()>0) // IF LENGTH IS GREATER THAN 0 ACCEPT IT ELSE REJECT IT
break;
else
cout<<"It can't be empty, enter again!!"<<endl;
}
return s;
}
void add(Customer customer[],int n){ // ADD FUNCTION WILL HELP ADDING A CUSTOMER TO INDEX N
cout<<"Enter name: "; // ASK FOR ALL THE ATTRIBUTES ONE BY ONE AND STORE
customer[n].name = take_string_input();
cout<<"Enter address: ";
customer[n].address = take_string_input();
cout<<"Enter city: ";
customer[n].city = take_string_input();
cout<<"Enter state: ";
customer[n].state = take_string_input();
cout<<"Enter zip: ";
customer[n].zip = take_int_input();
cout<<"Enter telephone number : ";
customer[n].telephoneNumber = take_int_input();
cout<<"Enter account balance: ";
customer[n].accountBalance = take_int_input();
cout<<"Enter date of last payment (in year/month/day format)";
customer[n].dateOfLastPayment = take_string_input();
cout<<"successfully added!!\n"<<endl;
}
void modify(Customer customer[],int n){ // MODIFY FUNCTION WHICH WILL MODIFY THE ELEMENTS FOR CUSTOMER
if (n==0){
cout<<"no data to modify\n"<<endl;
return;
}
string customerName;
cout<<"Enter name of the customer to change elements: ";
cin>>customerName; // ASK FOR THE NAME TO MODIFY THE CUSTOMER DETAILS
bool found = false;
for(int i = 0;i<n;i++){
if (customer[i].name==customerName){ // IF THE NAME IS FOUND IN THE CUSTOMER ARRAY, THEN GIVE A MENU DRIVEN TO MODIFY DETAILS IN THE SAME WAY AS ADD ACCOUNT
found = true;
cout<<"1. Modify name"<<endl;
cout<<"2. Modify address"<<endl;
cout<<"3. Modify city"<<endl;
cout<<"4. Modify state"<<endl;
cout<<"5. Modify zip"<<endl;
cout<<"6. Modify telephone number"<<endl;
cout<<"7. Modify account balance"<<endl;
cout<<"8. Modify date of last payment"<<endl;
cout<<"9. Go back"<<endl;
int ch;
cout<<"\nEnter your choice: ";
ch = take_int_input();
bool updated = false;
switch(ch){
case 1:
cout<<"Enter new name: ";
customer[i].name = take_string_input();
updated = true;
break;
case 2:
cout<<"Enter new address: ";
customer[i].address = take_string_input();
updated = true;
break;
case 3:
cout<<"Enter new city: ";
customer[i].city = take_string_input();
updated = true;
break;
case 4:
cout<<"Enter new state: ";
customer[i].state = take_string_input();
updated = true;
break;
case 5:
cout<<"Enter new zip: ";
customer[i].zip = take_int_input();
updated = true;
break;
case 6:
cout<<"Enter new telephone number: ";
customer[i].telephoneNumber = take_int_input();
updated = true;
break;
case 7:
cout<<"Enter new account balance: ";
customer[i].accountBalance = take_int_input();
updated = true;
break;
case 8:
cout<<"Enter new date of last payment: ";
customer[i].dateOfLastPayment = take_string_input();
updated = true;
break;
case 9:
break;
default:
cout<<"invalid choice!!\n"<<endl;
break;
}
if (updated){
cout<<"Successfully Updated \n"<<endl;
}
break;
}
}
if (!found){ // IF CUSTOMER NOT FOUND JUST OUTPUT NO SUCH CUSTOMER FOUND
cout<<"No such customer found!\n"<<endl;
}
}
bool compare(Customer a, Customer b){ // COMPARE FUNCTION TO SORT THE CUSTOMERS IN INCREASING ORDER OF THEIR NAME
return a.name<b.name;
}
void display(Customer customer[],int n){ // FUNCTION TO DISPLAY ALL THE CUSTOMER IN SORTED ORDER BY THEIR NAME
if (n==0){
cout<<"no data to display\n"<<endl;
}else{
sort(customer,customer+n,compare); // SORT THE CUSTOMER ARRAY BY THEIR NAME, AND OUTPUT ONE BY ONE
cout<<"Name\tAddress\tCity\tState\tZip\tTelephone\tAccount Balance\tDate of last payment"<<endl;
for(int i=0;i<n;i++){
cout<<customer[i].name<<"\t"<<customer[i].address<<"\t"<<customer[i].city<<"\t"<<customer[i].state<<"\t"<<customer[i].zip<<"\t"<<customer[i].telephoneNumber<<"\t\t"<<customer[i].accountBalance<<"\t\t"<<customer[i].dateOfLastPayment<<endl;
}
cout<<"\n"<<endl;
}
}
int main(){ // MAIN FUNCTION
Customer customer[10]; // MADE AN ARRAY OF MAX 10 CUSTOMER, ATLEAST 5 WAS MENTIONED IN THE PROBLEM STATEMENT
int n=0;
while (1){
cout<<"1. Add an account."<<endl; // SHOW A MENU DRIVEN PROGRAM WHAT THE USER WANT TO DO, AND ACCORDINGGLY JUST CALL THE FUNCTION
cout<<"2. Modify an account."<<endl;
cout<<"3. Display all the data sorted in the array."<<endl;
cout<<"4. Exit"<<endl;
int ch;
cout<<"\nEnter your choice: ";
ch = take_int_input();
switch(ch){
case 1:
if (n>10){
cout<<"Limit exceeded can't add more!"<<endl;
}
else{
add(customer,n);
n+=1;
}
break;
case 2:
modify(customer,n);
break;
case 3:
display(customer,n);
break;
case 4:
exit(0);
break;
default:
cout<<"Invalid choice\n"<<endl;
break;
}
}
return 0;
}
SCREENSHOT(SAMPLE OUTPUT):
NOTE: If you have any doubts regarding the solution please ask in the comment section! HAPPY LEARNING