카테고리 없음

Exercise 11.3: Structure

07514673 2021. 12. 20. 14:46

Exercise 11.3: Structure

Here is the problem specification:

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 10 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.

Input Validation: 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.


Sample Run

1. Enter new account information
2. Change account information
3. Display all account information
4. Exit the program

Enter your choice: 1

Customer name: Henry Smith
Customer address: 123 Stevens Creek Blvd
City: Cupertino
State: CA
ZIP code: 95014
Telephone: 408-111-1111
Account balance: 200
Date of last payment: 03/25/2018
You have entered informationfor customer number 0
1. Enter new account information
2. Change account information
3. Display all account information
4. Exit the program

Enter your choice: 3
Customer name: Henry Smith
Customer address: 123 Stevens Creek Blvd
City: Cupertino
State: CA
Zip: 95014
Telephone: 408-111-1111
Account balance: $200.00
Date of last payment: 03/25/2018

Press enter to continue...
1. Enter new account information
2. Change account information
3. Display all account information
4. Exit the program

Enter your choice: 2
Customer number: 0
Customer name: Henry Smith
Customer address: 123 Stevens Creek Blvd
City: Cupertino
State: CA
Zip: 95014
Telephone: 408-111-1111
Account balance: $200.00
Date of last payment: 03/25/2018

Customer name: Paul Rogers
Customer address: 123 main st
City: san jose
State: Ca
ZIP code: 95125
Telephone: 408-222-2222
Account balance: 100
Date of last payment: 05/02/2017
1. Enter new account information
2. Change account information
3. Display all account information
4. Exit the program

Enter your choice: 3
Customer name: Paul Rogers
Customer address: 123 main st
City: san jose
State: Ca
Zip: 95125
Telephone: 408-222-2222
Account balance: $100.00
Date of last payment: 05/02/2017

Press enter to continue...
1. Enter new account information
2. Change account information
3. Display all account information
4. Exit the program

Enter your choice: 4

 

#include <iostream>
#include <string>
using namespace std;
struct customer {
    string Name;
    string Address;
    string City;
    string State;
    string ZIP;
    string PhoneNumber;
    string AccountBalance;
    string LastPaymentDate;
};
void printMenu() {
    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;
}
int getEmptyPosition(customer customers[10]) {
    for (int i = 0; i < sizeof(customers); i++) {
        if (customers[i].Name == "") {
            return i;
        }
        else {
            return 10;
        }
    }
}
void choice1(customer customers[10], int pos) {
    
    cout << "Customer name: ";
    string temp;
    getline(cin,temp);
    getline(cin, customers[pos].Name);
    cout << "Customer address: ";
    getline(cin, customers[pos].Address);
    cout << "City: ";
    getline(cin, customers[pos].City);
    cout << "State: ";
    getline(cin, customers[pos].State);
    cout << "ZIP Code: ";
    getline(cin, customers[pos].ZIP);
    cout << "Telephone: ";
    getline(cin, customers[pos].PhoneNumber);
    cout << "Account balance: ";
    getline(cin, customers[pos].AccountBalance);
    cout << "Date of last payment: ";
    getline(cin, customers[pos].LastPaymentDate);
    cout << "You have entered information for customer number " << pos << endl << 
endl;
}
void choice2(customer customers[10]) {
    int pos;
    cout << "Customer number: ";
    cin >> pos;
    cout << "Customer name: ";
    string temp;
    getline(cin, temp);
    getline(cin, customers[pos].Name);
    cout << "Customer address: ";
    getline(cin, customers[pos].Address);
    cout << "City: ";
    getline(cin, customers[pos].City);
    cout << "State: ";
    getline(cin, customers[pos].State);
    cout << "ZIP Code: ";
    getline(cin, customers[pos].ZIP);
    cout << "Telephone: ";
    getline(cin, customers[pos].PhoneNumber);
    cout << "Account balance: ";
    getline(cin, customers[pos].AccountBalance);
    cout << "Date of last payment: ";
    getline(cin, customers[pos].LastPaymentDate);
    cout << "You have entered information for customer number " << pos << endl << 
endl;
}
void choice3(customer customers[10]) {
    for (int pos = 0; pos < sizeof(customers); pos++) {
        if (customers[pos].Name != "") {
            cout << "Customer name: " << customers[pos].Name << endl;
            cout << "Customer address: " << customers[pos].Address << endl;
            cout << "City: " << customers[pos].City << endl;
            cout << "State: " << customers[pos].State << endl;
            cout << "ZIP Code: " << customers[pos].ZIP << endl;
            cout << "Telephone: " << customers[pos].PhoneNumber << endl;
            cout << "Account balance: " << customers[pos].AccountBalance << endl;
            cout << "Date of last payment: " << customers[pos].LastPaymentDate << 
endl << endl;
            
        }
    }
    
}
int main()
{
    customer customers[10];
    int userChoice;
    int pos = 0;
    do {
        printMenu();
        
        cout << "\nEnter Your Choice: ";
        cin >> userChoice;
        cout << endl;
        
        if (userChoice == 1) {
            choice1(customers, pos);
            pos++;
        }
        else if (userChoice == 2) {
            choice2(customers);
        }
        else if (userChoice == 3) {
            choice3(customers);
        }
    } while (userChoice >= 1 && userChoice <= 3);
    
    return 0;
}