Question: (FIX MY CODE) please 11.7: Customer Accounts (C++) Write a program that uses a structure to store the following data about a customer account: Customer

(FIX MY CODE) please

11.7: Customer Accounts (C++) Write a program that uses a structure to store the following data about a customer account: Customer name Customer address City State ZIP code Telephone Account balance Date of last payment

The program should use an array of at least 20 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 user interface.

Prompts And Output Labels. Your main menu should be the following: 1. Enter new account information 2. Change account information 3. Display all account information 4. Exit the program The user is expected to enter 1 or 2 or 3 or 4. The main menu is displayed at the start of the program and after the handling of choices 1, 2 and 3. If 1 is entered for the main menu, the program prompts for each of the data listed above, in the order listed above, using the above data descriptions (e.g. "ZIP code") as prompts (followed in each case by a colon). After reading in and processing the data, the program prints You have entered information for customer number X where X is the customer number: 0 for the first customer and increasing by 1 for each subsequent customer that is entered. If 2 is entered for the main menu, the program prompts for the customer number: Customer number: Upon entering a valid customer number the program displays all the data for the particular customer that has been saved: Customer name : ... Customer address: ... City: ... State: ... ZIP code: ... Telephone: ... Account balance: ... Date of last payment: ... The program then skips one or two lines and prompts for a change, using the same prompts as in choice 1 above for all the data items associated with a customer. If 3 is entered for the main menu, the program displays all the data for each customer that has been saved, using the display format in choice 2 above. After the display of each customer the program prompts "Press enter to continue..." and waits for the user to hit return. If 4 is entered for the main menu, the program terminates.

Input Validation (OPTIONAL).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.

MY CODE:

#include #include #include #include #include // for streamsize max, to flush cin buffer #include // for tolower

using std::cout; using std::cin; using std::cerr; using std::string; using std::vector; using std::tolower; using std::getline;

// helper functions to clear the cin buffer void resetCin() { cin.ignore(std::numeric_limits::max(), ' '); cin.clear(); }

struct Customer { private: string name; string address; string phone; float balance; string last_payment_date;

public: // default constructor uses interactive input Customer() { setName(); setAddress(); setPhone(); setBalance(); setLastPayDate(); }

// member mutators -- never allow members to be empty void setName() { do { cout << "Enter the customer's name: "; getline(cin, name, ' '); } while (name.empty()); } void setAddress() { do { string line; // hold each line of input cout << "Enter the address (enter a blank line to finish): ";

// gather address one line at a time, address member will have at // least one newline (in all likelihood), can have many lines. do { line.clear(); getline(cin, line, ' '); address.append(line); address.push_back(' '); } while ( !line.empty() );

// remove trailing newline characters address = address.substr(0, address.rfind(" ")); address = address.substr(0, address.rfind(" ")); } while (address.empty()); } void setPhone() { do { cout << "Enter the customer's phone number: "; getline(cin, phone, ' '); } while (phone.empty()); } void setBalance() { balance = -1; // use bad value in case extraction fails first time do { cout << "Enter the customer's account balance " "(must not be negative): $ "; cin >> balance; } while (balance < 0); resetCin(); } void setLastPayDate() { do { cout << "Enter the date of customer's last payment: "; getline(cin, last_payment_date, ' '); } while (last_payment_date.empty()); }

// member accessors string getName() { return name; } string getAddress() { return address; } string getPhone() { return phone; } float getBalance() { return balance; } string getLastPayDate() { return last_payment_date; }

void print() { cout << name << " " << address << " Phone Number: " << phone << " Account Balance: $" << balance << " " << "Date of Last Payment: " << last_payment_date << " "; } };

// define menu options const char MENU_EXIT = 'q'; const char MENU_ADD = 'i'; const char MENU_EDIT = 'e'; const char MENU_PRINT = 'p'; const char MENU_PRINT_ALL = 'a';

char prompt() { char c = 'H'; // nonselection by default, in case extraction fails cout << "---------------------------------------------------- " "I = Insert Customer | E = Edit Existing Customer " "P = Print Customer | A = Print All Customer Entries " "Q = Quit " "---------------------------------------------------- " "selection> "; cin >> c; resetCin(); return tolower(c); }

char edit_prompt() { char c = 'H'; // nonselection by default, in case extraction fails cout << "---------------------------------------------------- " "N = Name | A = Address | P = Phone Number " "B = Account Balance | D = Date of Last Payment " "Q = Done Editing this Customer " "---------------------------------------------------- " "edit> "; cin >> c; resetCin(); return tolower(c); }

void editCustomer(vector& d, unsigned index) { if (index >= d.size() or index < 0) { cerr << "** bad index given to editCustomer [" << index << "]. " "valid range is 0 to " << d.size() - 1 << ". ** "; return; }

cout << ' '; d.at(index).print(); // show the customer being edited for( char c = edit_prompt(); c != MENU_EXIT; c = edit_prompt()) { switch (c) { case 'n': d.at(index).setName(); break; case 'a': d.at(index).setAddress(); break; case 'p': d.at(index).setPhone(); break; case 'b': d.at(index).setBalance(); break; case 'd': d.at(index).setLastPayDate(); break; } d.at(index).print(); }

}

// contains all customer data vector data;

int main() { // set formatting for entire program cout << std::fixed << std::setprecision(2);

for (char selection = prompt(); selection != MENU_EXIT; selection = prompt()) { unsigned customer_index; // used in MENU_EDIT and MENU_PRINT cases

switch (selection) { case MENU_ADD: cout << "Adding Customer " << data.size() + 1 << ". "; data.push_back(Customer()); break;

case MENU_EDIT: if (data.size() == 0) break; // don't try to edit empty data cout << "Enter the customer index to edit [1 to " << data.size() << "]: "; cin >> customer_index; customer_index--; // actual index will be 1 lower editCustomer(data, customer_index); break;

case MENU_PRINT: if (data.size() == 0) break; // don't try to display empty data cout << "Enter the customer index to display [1 to " << data.size() << "]: "; cin >> customer_index; customer_index--; if (customer_index >= data.size() or customer_index < 0) { cerr << "** bad index [" << customer_index << "]. " "valid range is 0 to " << data.size()-1 << ". ** "; } else { data.at(customer_index).print(); } break;

case MENU_PRINT_ALL: cout << ' '; for (unsigned i = 0; i < data.size(); i++) { cout << "Customer " << i+1 << " " "-------------- "; data.at(i).print(); cout << ' '; } break; } }

MY ERROR:

COMPILER ERROR MESSAGES

CTest.cpp: In function int main(): CTest.cpp:220:5: error: expected } at end of input } ^ 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!