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

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...
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...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a C++ program that uses array to store the salaries of 10 employees working in...
Write a C++ program that uses array to store the salaries of 10 employees working in a small firm. The program should take average of the salaries and the max and min salaries being paid to the employees
Write a program that declares a struct to store the data of a football player (player’s...
Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of a...
Write a C program that contains a structure that uses predefined types and union. • Create...
Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adult), and kindOfPerson (Either kid, student, or adult) • kids have a school field. Students have college and gpa. Adults have company and salary. • Create one non-dynamic struct with the content for a college student: "Bob", 20, K-State, 3.5 • Create one struct dynamically for a kid with: "Alison", 10, "Amanda Arnold...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT