1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int MAXCUST = 30; // Constant value for the maximum number of customers possible
struct Customer
{
int id; // id
string name; // Name
string zip; // ZIP code
double balance; // Account balance
string lastPay; // Date of last payment
};
int menu(); // Function prototype for menu
void addcust(Customer CD[MAXCUST], int NumCust); // Function prototype for addcust (Add customer)
void updatecust(Customer CD[MAXCUST], int NumCust); // Function prototype updatecust (Update customer)
void displaycust(Customer CD[MAXCUST], int NumCust); // Function prototype for displaycust(Display customer)
int findcust(Customer CD[MAXCUST], int id, int NumCust); // Function prototype findcust(Find customer)
int main(){
int NumCust; // Declares NumCust, the number of customers in the array
int choice; // Declares choice, the literal choice for the menu, 1-4
Customer CD[MAXCUST]; // Declares the array of structs at a size of MAXCUST which is = to 30
menu(); // Call for menu, which displays the menu
while (choice != 4){ // While loop to run the menu program untill 4 is returned
if (choice == 1){ // Option for choice 1
if (NumCust < MAXCUST){ // Validation to make sure the array isnt full already
addcust(CD, &NumCust); // call for addcust
}
}
else if (choice == 2){
updatecust(CD, NumCust); // call for update cust, which is called when 2 is chosen and thus returned by the menu function
}
else if (choice == 3){
displaycust(CD, NumCust); // call for displaycust, which is called when 3 is chosen and thus returned by the menu function
}
}
return 0;
}
int menu(){ // Function for menu
int NumCust; // Declares NumCust
int choice; // Declares choice
cout << "1. Enter new account information\n";
cout << "2. Change account information\n";
cout << "3. Display all account information\n";
cout << "4. Exit the program\n\n";
// The options displayed for the user to read
cout << "Enter your choice: ";
cin >> choice; // Allows for the user to input their choice
if (choice >= 1 && choice <= 4) // Validates that the choice is between 1 and 4
return choice; // Returns the choice; a number between 1 and 4
else
{
cout << "Invalid choice, enter again"; // Displays if the choice is outside of the valid choices
}
}
void addcust(Customer CD[MAXCUST], int &NumCust){ // Function for addcust
int id;
string name; // Declares id, name, zip, balance, and last pay
string zip;
double balance;
string lastPay;
cout << "Please enter ID number"; // Asks for ID number
cin >> CD[NumCust].id; // Allows user to input id number which goes into the specific customer's struct
cout << "Please enter name"; // Asks for name
cin >> CD[NumCust].name; // Allows user to input name which goes into the specific customer's struct
cout << "Please enter zipcode"; // Asks for zipcode
cin >> CD[NumCust].zip; // Allows user to input zip code which goes into the specific customer's struct
cout << "Enter account balance"; // Asks for account balance
cin >> CD[NumCust].balance; // Allows user to input balance which goes into the specific customer's struct
if (CD[NumCust].balance <= 0){ // Validation to check that the balance is 0 or higher
cout << "Error, balance must be 0 or greater. Enter again"; // Error message that displays when a negative number is shown
cin >> CD[NumCust].balance;
}
cout << "Enter date of last payment"; // Asks for date of last payment
cin >> CD[NumCust].lastPay; // Allows user to input last payment date which goes into the specific customer's struct
cin.ignore();
}
void updatecust(Customer CD[MAXCUST], int NumCust){ // Function for updatecust
int id;
int temp;
cout << "Please enter ID number"; // Asks for an id number (Pre-existing)
cin >> id;
temp = findcust(CD, id, NumCust); // Calls findcust and assigns its value to temp
cout << "Please enter name"; // Asks for name
cin >> CD[NumCust].name; // Allows user to input name which goes into the specific customer's struct
cout << "Please enter zipcode"; // Asks for zipcode
cin >> CD[NumCust].zip; // Allows user to input zip code which goes into the specific customer's struct
cout << "Enter account balance"; // Asks for account balance
cin >> CD[NumCust].balance; // Allows user to input balance which goes into the specific customer's struct
if (CD[NumCust].balance <= 0){ // Validation to check that the balance is 0 or higher
cout << "Error, balance must be 0 or greater. Enter again"; // Error message that displays when a negative number is shown
cin >> CD[NumCust].balance;
}
cout << "Enter date of last payment"; // Asks for date of last payment
cin >> CD[NumCust].lastPay; // Allows user to input last payment date which goes into the specific customer's struct
cin.ignore();
}
void displaycust(Customer CD[MAXCUST], int NumCust){ //Function for display cust, which displays the customers and their info
cout << left << setw(15) << "Customer ID" << setw(15) << "Customer Name" << setw(15) << "Zip Code"
<< setw(15) << "Balance" << setw(15) << "Date Last Paid" << setw(15) << endl;
cout << setw(15) << "---------" << setw(15) << "-----------" << setw(15) << "------"
<< setw(15) << "-------" << setw(15) << "---------" << setw(15) << "----------" << endl;
cout << CD[NumCust].id << setw(15) << CD[NumCust].name << setw(15) << CD[NumCust].zip << setw(15);
cout << CD[NumCust].balance << setw(15) << CD[NumCust].lastPay << setw(15) << endl;
cout << "-------------------------------------------------------------------------------------" << endl;
}
int findcust(Customer CD[MAXCUST], int id, int NumCust){ // Function for findcust, which finds an existing customer from their id
int temp = -1;
for (int k = 0; k < NumCust; k++) { // For loop to run through the existing information
if (id == CD[k].id){ // Matches the id to an existing id if there is one
temp = k; // Assigns the subscript of the matching id to temp
}
}
return temp;
}
|