Question

In: Computer Science

Add a function to Programming Challenge 7 that allows the user to search the structure array...

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?



Solutions

Expert Solution

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:


Related Solutions

Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
Add Instance to the Array List Instances in an Array Search for an Instance in the...
Add Instance to the Array List Instances in an Array Search for an Instance in the Array Delete an Instance from the Array Update an Instance in the Array Save Array Elements to CSV File Exit Program The requirement of this assignment is to design and implement a system in Java that will manage instantiated instances of the Objects created in the User Designed Object Assignment. This is the Young Adult Object. The system should be able to add an...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
goal of this function will be to add an element to an already existing array in...
goal of this function will be to add an element to an already existing array in C/C++. bool add(structure dir[], int& size, const char nm[], const char ph[]){ //code here } add = function name structure = structure with two fields { char name[20]; char phone[13]; } int& size = # of entries in dir nm = name that's going to be added ph = phone that's going to be added. return true if entry was successfully added, false if...
Python Add a command to this chapter’s case study program that allows the user to view...
Python Add a command to this chapter’s case study program that allows the user to view the contents of a file in the current working directory. When the command is selected, the program should display a list of filenames and a prompt for the name of the file to be viewed. Be sure to include error recovery in the program. If the user enters a filename that does not exist they should be prompted to enter a filename that does...
Write the code (in a function) that allows the user to display the first “nb” of...
Write the code (in a function) that allows the user to display the first “nb” of elements in the Fibonacci sequence (each value on a separate line in the console). The order of the Fibonacci sequence goes as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and on to infinity. Each number is the sum of the previous two (with the 2 starting numbers being 1 and 1). This series of numbers is known as...
C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping...
C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping cart and get the total price of purchases made. Items are simply described by an Item class as follows: class Item {   public:      std :: String description;      float price; }; The ShopCart class must be able to add and remove items and display an invoice. This class must use a dynamically allocated array of items whose capacity is fixed in advance to the build....
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT