Question

In: Computer Science

Write a program that uses a structure to store the following data about a customer account:...

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++

Solutions

Expert Solution

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


Related Solutions

11.7: Customer Accounts Write a program that uses a structure to store the following data about...
11.7: Customer Accounts 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...
Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
Write a program that uses a structure to store the following data: (Remember to use proper...
Write a program that uses a structure to store the following data: (Remember to use proper formatting and code documentation) Member Name Description name student name idnum Student ID number Scores [NUM_TESTS] an array of test scores Average Average test score Grade Course grade Declare a global const directly above the struct declaration const int NUM_TESTS = 4; //a global constant The program should ask the user how many students there are and should then dynamically allocate an array of...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
Write a program for a beauty store that uses an InputBox to ask the guest for...
Write a program for a beauty store that uses an InputBox to ask the guest for a membership code. Embed this in a Do loop so that the user has to keep trying until the result is a valid membership code that starts with “Beauty” (case insensitive) and is followed by 4 digits with the final digit equal to either 6 or 8. Then use a message box to display the input promotion code and inform the user that the...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
Python: Write a program to simulate the purchases in a grocery store. A customer may buy...
Python: Write a program to simulate the purchases in a grocery store. A customer may buy any number of items. So, you should use a while loop. Your program should read an item first and then read the price until you enter a terminal value (‘done’) to end the loop. You should add all the prices inside the loop. Add then a sales tax of 8.75% to the total price. Print a receipt to indicate all the details of the...
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the...
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT