In: Computer Science
Add a function to Programming Challenge 7 that allows the user
to search the structure array for a particular customer's account.
It should accept part of the customer's name as an argument and
then search for an account with a name that matches it. All
accounts that match should be displayed. If no account matches, a
message saying so should be displayed. Currently I have
#include <iostream>
#include <string>
using namespace std;
void displayAccount(struct customerAccount Arr[], int index);
void changeAccount(struct customerAccount Arr[], int index, int
num);
void createNewAccount(struct customerAccount Arr[], int
index);
void menu();
const int SIZE = 2;
struct customerAccount
{
string name, address, city, state, zip, phone,
date;
double balance;
};
int main()
{
int choice = 0;
int num = 0;
int index = 1;
customerAccount Arr[2];
menu();
cout << "Please enter a choice: 1-4\n";
cin >> choice;
{
switch (choice)
{
case 1:
createNewAccount(Arr, index);
index++;
cout <<
"Make another choice 1-4: \n";
cin >>
choice;
case 2:
cout <<
"Enter the customer number you would like to change the account
information of " << endl;
cin >>
num;
changeAccount(Arr, num, index);
break;
case 3:
displayAccount(Arr, choice);
system("pause");
case 4:
return 0;
default:
cout <<
"Please enter a valid choice ";
cout <<
"between 1 and 4:" << endl;
menu();
cout <<
"Please enter a choice: ";
cin >>
choice;
}
}
return 0;
}
void menu()
{
cout << "****************************" <<
endl;
cout << "\n \t MENU \n" << endl;
cout << "****************************" <<
endl;
cout << "1. Enter new account information"
<< endl;
cout << "2. Change account information" <<
endl;
cout << "3. Display all account information"
<< endl;
cout << "4. Exit the program" <<
endl;
}
void displayAccount(struct customerAccount Arr[], int index)
{
for (int i = 0; i < SIZE; i++)
{
cout << "The Customer's name
is " << Arr[i].name << endl;
cout << "The Customer's
address is " << Arr[i].address << endl;
cout << "Customer City is "
<< Arr[i].city << ", ";
cout << Arr[i].state <<
"\n Area code: " << Arr[i].zip << endl;
cout << "Their phone number
is " << Arr[i].phone << endl;
cout << "Their account
creation date is " << Arr[i].date << endl;
cout << "Their account
balance is " << Arr[i].balance << endl;
}
}
void changeAccount(struct customerAccount Arr[], int index, int
num)
{
cout << "enter the new Customer name:
";
cin.ignore();
getline(cin, Arr[num].name);
cout << "enter the new Customer address:
";
getline(cin, Arr[num].address);
cout << "enter the new City: ";
getline(cin, Arr[num].city);
cout << "enter the new State: ";
getline(cin, Arr[num].state);
cout << "enter the new ZIP Code: ";
getline(cin, Arr[num].zip);
cout << "enter their telephone number: ";
getline(cin, Arr[num].phone);
cout << "Account balance, postive numbers only:
";
cin >> Arr[num].balance;
if (Arr[num].balance < 0)
{
cout << "this is not a valid
balance.\n";
cout << "please enter another
balance";
cin >>
Arr[num].balance;
}
cout << "enter a date of last payment: ";
cin.ignore();
getline(cin, Arr[num].date);
}
void createNewAccount(customerAccount Arr[], int index)
{
cout << "enter a Customer name: ";
cin.ignore();
getline(cin, Arr[index].name);
cout << "enter a Customer address: ";
getline(cin, Arr[index].address);
cout << "enter their City: ";
getline(cin, Arr[index].city);
cout << "enter their State: ";
getline(cin, Arr[index].state);
cout << "enter their ZIP Code: ";
getline(cin, Arr[index].zip);
cout << "Telephone: ";
getline(cin, Arr[index].phone);
cout << "Account balance: ";
cin >> Arr[index].balance;
cout << "Date of last payment: ";
cin.ignore();
getline(cin, Arr[index].date);
}
How do I make a sort function for this?
Code:
#include <iostream>
#include <string>
using namespace std;
void displayAccount(struct customerAccount Arr[], int index);
void changeAccount(struct customerAccount Arr[], int index, int
num);
void createNewAccount(struct customerAccount Arr[], int
index);
void searchAccount(struct customerAccount Arr[], string
name);
void sortAccount(struct customerAccount Arr[]);
void menu();
const int SIZE = 2;
struct customerAccount
{
string name, address, city, state, zip, phone, date;
double balance;
};
int main()
{
int choice = 0;
int num = 0;
int index = 1;
customerAccount Arr[2];
string name;
do
{
menu();
cout << "Please enter a choice: 1-4\n";
cin >> choice;
switch (choice)
{
case 1:
createNewAccount(Arr, index);
index++;
cout << "Make another choice 1-4: \n";
break;
case 2:
cout << "Enter the customer number you would like to change
the account information of " << endl;
cin >> num;
changeAccount(Arr, num, index);
break;
case 3:
displayAccount(Arr, choice);
system("pause");
break;
case 4:
cout << "Enter the customer name you would like to search the
account information of " << endl;
cin >> name;
searchAccount(Arr, name);
break;
case 5:
sortAccount(Arr);
cout<<"Accounts are sorted."<<endl;
break;
case 6:
return 0;
default:
cout << "Please enter a valid choice ";
cout << "between 1 and 4:" << endl;
break;
}
}while(1);
return 0;
}
void menu()
{
cout << "\n****************************" << endl;
cout << "\n \t MENU \n" << endl;
cout << "****************************" << endl;
cout << "1. Enter new account information" <<
endl;
cout << "2. Change account information" << endl;
cout << "3. Display all account information" <<
endl;
cout << "4. Search an account" << endl;
cout << "5. Sort the accounts" << endl;
cout << "6. Exit the program\n" << endl;
}
void displayAccount(struct customerAccount Arr[], int index)
{
for (int i = 0; i < SIZE; i++)
{
cout << "The Customer's name is " << Arr[i].name
<< endl;
cout << "The Customer's address is " << Arr[i].address
<< endl;
cout << "Customer City is " << Arr[i].city << ",
";
cout << Arr[i].state << "\n Area code: " <<
Arr[i].zip << endl;
cout << "Their phone number is " << Arr[i].phone
<< endl;
cout << "Their account creation date is " <<
Arr[i].date << endl;
cout << "Their account balance is " << Arr[i].balance
<< endl;
}
}
void changeAccount(struct customerAccount Arr[], int index, int
num)
{
cout << "enter the new Customer name: ";
cin.ignore();
getline(cin, Arr[num].name);
cout << "enter the new Customer address: ";
getline(cin, Arr[num].address);
cout << "enter the new City: ";
getline(cin, Arr[num].city);
cout << "enter the new State: ";
getline(cin, Arr[num].state);
cout << "enter the new ZIP Code: ";
getline(cin, Arr[num].zip);
cout << "enter their telephone number: ";
getline(cin, Arr[num].phone);
cout << "Account balance, postive numbers only: ";
cin >> Arr[num].balance;
if (Arr[num].balance < 0)
{
cout << "this is not a valid balance.\n";
cout << "please enter another balance";
cin >> Arr[num].balance;
}
cout << "enter a date of last payment: ";
cin.ignore();
getline(cin, Arr[num].date);
}
void createNewAccount(customerAccount Arr[], int index)
{
cout << "enter a Customer name: ";
cin.ignore();
getline(cin, Arr[index].name);
cout << "enter a Customer address: ";
getline(cin, Arr[index].address);
cout << "enter their City: ";
getline(cin, Arr[index].city);
cout << "enter their State: ";
getline(cin, Arr[index].state);
cout << "enter their ZIP Code: ";
getline(cin, Arr[index].zip);
cout << "Telephone: ";
getline(cin, Arr[index].phone);
cout << "Account balance: ";
cin >> Arr[index].balance;
cout << "Date of last payment: ";
cin.ignore();
getline(cin, Arr[index].date);
}
void searchAccount(struct customerAccount Arr[], string name)
{
bool f=0;
for (int i = 0; i < SIZE; i++)
{
if(Arr[i].name == name)
{
f=1;
cout << "The Customer's name is " << Arr[i].name
<< endl;
cout << "The Customer's address is " << Arr[i].address
<< endl;
cout << "Customer City is " << Arr[i].city << ",
";
cout << Arr[i].state << "\n Area code: " <<
Arr[i].zip << endl;
cout << "Their phone number is " << Arr[i].phone
<< endl;
cout << "Their account creation date is " <<
Arr[i].date << endl;
cout << "Their account balance is " << Arr[i].balance
<< endl;
cout << endl;
}
}
if(f==0)
cout << "No such name exists!" << endl;
}
void sortAccount(struct customerAccount Arr[])
{
int i, j;
for (i = 0; i < SIZE-1; i++)
// Last i elements are already in place
for (j = 0; j < SIZE-i-1; j++)
if (Arr[j].balance > Arr[j+1].balance)
{
struct customerAccount temp;
temp = Arr[j];
Arr[j] = Arr[j+1];
Arr[j+1] = temp;
}
}
Screenshots: