// Included Libraries and namespace #include #include #include #include "A:\string.h" #include "A:\string" using namespace std; // Function Prototypes void printBook(string [], int); void printRecord(string [], string [], string [], int); int addData(string [], string [], string [], int); void updateData(string [], string [], string [], int); int deleteData(string [], string [], string [], int); void sortData(string [], string [], string [], int); char menu(); int subMenu(string [], int); int initializer(string [], string [], string []); // Main Program int main() { // Declare variables and files string name[20] = {""}, address[20] = {""}, city[20] = {""}; int numRec, count; char choice; ofstream outFile; // Initialize function numRec = initializer(name, address, city); // Sort Array sortData(name, address, city, numRec); // Menu and switch choice = menu(); while (choice != '6') { switch(choice) { // Print all records case '1': printBook(name, numRec); system("pause"); break; // Print record case '2': system("cls"); cout << "\n\n\n\n\n"; cout << "\t\t######################################\n"; cout << "\t\t# #\n"; cout << "\t\t# View Menu #\n"; printRecord(name, address, city, numRec); break; // Add record case '3': numRec = addData(name, address, city, numRec); break; // Delete record case '4': system("cls"); cout << "\n\n\n\n\n"; cout << "\t\t######################################\n"; cout << "\t\t# #\n"; cout << "\t\t# Delete Menu #\n"; numRec = deleteData(name, address, city, numRec); break; // Update record case '5': system("cls"); cout << "\n\n\n\n\n"; cout << "\t\t######################################\n"; cout << "\t\t# #\n"; cout << "\t\t# Update Menu #\n"; updateData(name, address, city, numRec); break; } choice = menu(); } // Write the data to the file cout << "\n Writing records to file... Please wait...\n\n"; outFile.open("A:address.txt"); outFile << numRec << endl; for(count = 0; count < numRec; count++) outFile << name[count] << "#" << address[count] << "#" << city[count] << endl; outFile.close(); // Return and end of Main program system("cls"); return 0; } // ********** Functions ************ // *********** printBook function *********** void printBook(string name[], int numRec) { // Declaring variables int count = 0; // Print records to screen system("cls"); cout << "\n\n Record # Name" << endl << endl; for(count = 0; count < numRec; count++) cout << "\t" << count + 1 << ":\t" << name[count] << endl; cout << endl; // Return and end of printBook function return; } // *********** printRecord function *********** void printRecord(string name[], string address[], string city[], int numRec) { // Declaring variables string search; int found; // Submenu program found = subMenu(name, numRec); // Switch for menu switch (found) { // No record found case -1: cout << " *** Record not found! ***\n"; system("pause"); break; // Exit menu case -2: break; // Record found default: cout << endl; cout << endl; cout << " \t " << name[found] << endl; cout << " \t " << address[found] << endl; cout << " \t " << city[found] << endl; cout << endl; system("pause"); break; } // Return and end of printRecord function return; } // *********** addData function *********** int addData(string name[], string address[], string city[], int numRec) { // Declaring variables string temp1, temp2, temp3; // Check for too many records if (numRec == 20) cout << "Address Book full, you can't add another address!\n"; else { // Add record cout << "\n** ADD A RECORD **" << endl; // Last name cout << " Enter last name to add: "; cin.ignore(1); getline(cin, temp1); // First name cout << " Enter first name to add: "; getline(cin, temp2); // Update name array name[numRec] = temp1 + ", " + temp2; // Address and update address array cout << " Enter the address to add: "; getline(cin, address[numRec]); // City cout << " Enter the City: "; getline(cin, temp1); // State cout << " Enter the State: "; getline(cin, temp2); // Zip Code cout << " Enter the Zip Code: "; getline(cin, temp3); // Update city array city[numRec] = temp1 + ", " + temp2 + " " + temp3; // Update number of records numRec++; // Resort records sortData(name, address, city, numRec); } // Return and end of addData function return numRec; } // *********** deleteData function *********** int deleteData(string name[], string address[], string city[], int numRec) { // Declaring variables int count, found; char confirm; // Call submenu found = subMenu(name, numRec); // Switch for menu switch (found) { // Record not found case -1: cout << " *** Record not found! ***\n"; system("pause"); break; // Exit menu case -2: break; // Record found default: // Display record and confirm delete cout << "\n\n \t " << name[found] << endl; cout << " \t " << address[found] << endl; cout << " \t " << city[found] << endl << endl; cout << " *** Confirm Delete ***\n Enter 'Y' to continue with delete: "; cin >> confirm; // Delete record if (toupper(confirm) == 'Y') { cout << " *** Record deleted! ***\n"; numRec--; for(count = found; count < numRec; count++) { name[count] = name[count + 1]; address[count] = address[count + 1]; city[count] = city[count + 1]; } } // Don't delete record else cout << " *** No records changed ***\n"; system("pause"); break; } // Return and end of deleteData function return numRec; } // *********** updateData function *********** void updateData(string name[], string address[], string city[], int numRec) { // Declaring variables int found; char confirm; string temp1, temp2, temp3; // Call submenu found = subMenu(name, numRec); // Switch for menu switch (found) { // Record not found case -1: cout << " *** Record not found! ***\n"; system("pause"); break; // Exit menu case -2: break; // Record found default: // Display record and change data cout << "\n\n \t " << name[found] << endl; cout << " \t " << address[found] << endl; cout << " \t " << city[found] << endl << endl; cout << " *** Confirm Update ***\n Enter 'Y' to continue with delete: "; cin >> confirm; // update record if (toupper(confirm) == 'Y') { // Last name cout << " Enter last name to add: "; cin.ignore(1); getline(cin, temp1); // First name cout << " Enter first name to add: "; getline(cin, temp2); // Update name array name[found] = temp1 + ", " + temp2; // Address and update address array cout << " Enter the address to add: "; getline(cin, address[found]); // City cout << " Enter the City: "; getline(cin, temp1); // State cout << " Enter the State: "; getline(cin, temp2); // Zip Code cout << " Enter the Zip Code: "; getline(cin, temp3); // Update city array city[found] = temp1 + ", " + temp2 + " " + temp3; } // Don't update record else cout << " *** No records changed ***\n"; system("pause"); break; } // Return and end of updateData function return; } // *********** sortData function *********** void sortData(string name[], string address[], string city[], int numRec) { // Declare variables int count, counter, small; string temp, checker1, checker2; // sort records for(count = 0; count < numRec; count++) { small = count; for(counter = count + 1; counter < numRec; counter++) { checker1 = name[counter]; checker2 = name[small]; transform(checker1.begin(), checker1.end(), checker1.begin(), toupper); transform(checker2.begin(), checker2.end(), checker2.begin(), toupper); if(checker1 < checker2) small = counter; } temp = name[small]; name[small] = name[count]; name[count] = temp; temp = address[small]; address[small] = address[count]; address[count] = temp; temp = city[small]; city[small] = city[count]; city[count] = temp; } // Return and end of sortData function return; } // *********** menu function *********** char menu() { // Declaring variables string choice = ""; // Display menu while (!(choice > "0" && choice < "7")) { system("cls"); cout << "\n\n\n\n\n"; cout << "\t\t######################################\n"; cout << "\t\t# #\n"; cout << "\t\t# Address Book Menu #\n"; cout << "\t\t# #\n"; cout << "\t\t# 1: VIEW list of names #\n"; cout << "\t\t# 2: VIEW a record #\n"; cout << "\t\t# 3: ADD a record #\n"; cout << "\t\t# 4: DELETE a record #\n"; cout << "\t\t# 5: UPDATE a record #\n"; cout << "\t\t# 6: QUIT program #\n"; cout << "\t\t# #\n"; cout << "\t\t######################################\n"; cout << endl; cout << " Choice: "; cin >> choice; // Prompt for choice while (!(choice > "0" && choice < "7") || (choice.size() != 1)) { cout << " Choose a VALID command. Choice: "; cin >> choice; } cout << endl; } // Exit function with a char type variable if (choice.compare(0, 1, "1") == 0) return '1'; if (choice.compare(0, 1, "2") == 0) return '2'; if (choice.compare(0, 1, "3") == 0) return '3'; if (choice.compare(0, 1, "4") == 0) return '4'; if (choice.compare(0, 1, "5") == 0) return '5'; if (choice.compare(0, 1, "6") == 0) return '6'; return '0'; } // ********** subMenu function ************ int subMenu(string name[], int numRec) { // Declaring variables string search, choice1, tempName; int count, len, found; char choice; // Display menu while (!(choice1 > "0" && choice1 < "6")) { cout << "\t\t# #\n"; cout << "\t\t# 1: Search by Record Number #\n"; cout << "\t\t# 2: Search by Last Name #\n"; cout << "\t\t# 3: Search by First Name #\n"; cout << "\t\t# 4: Search by Full Name #\n"; cout << "\t\t# 5: Back to Main Menu #\n"; cout << "\t\t# #\n"; cout << "\t\t######################################\n"; cout << endl; cout << " Choice: "; cin >> choice1; // Prompt for choice while (!(choice1 > "0" && choice1 < "6") || (choice1.size() != 1)) { cout << " Choose a VALID command. Choice: "; cin >> choice1; } cout << endl; } // Change string variable to char variable using if statements if (choice1.compare(0, 1, "1") == 0) choice = '1'; if (choice1.compare(0, 1, "2") == 0) choice = '2'; if (choice1.compare(0, 1, "3") == 0) choice = '3'; if (choice1.compare(0, 1, "4") == 0) choice = '4'; if (choice1.compare(0, 1, "5") == 0) choice = '5'; // Set default found = -2; // Switch menu options switch(choice) { case '1': // Print all records and prompt for record number printBook(name, numRec); cout << "Record number to search for: "; cin >> found; // check to see if selection is valid found--; if (found < 0 || found > numRec - 1) found = -1; break; case '2': // Prompt for last name cout << endl; cout << " Enter Last name to search for: "; cin.ignore(1); getline(cin, search); transform(search.begin(), search.end(), search.begin(), toupper); // Search for last name found = -1; for(count = 0; count < numRec; count++) { tempName.assign(name[count], 0, name[count].find(",", 0)); transform(tempName.begin(), tempName.end(), tempName.begin(), toupper); len = name[count].size(); if(tempName.compare(0, len, search) == 0) found = count; } break; case '3': // Prompt for first name cout << endl; cout << " Enter First name to search for: "; cin.ignore(1); getline(cin, search); transform(search.begin(), search.end(), search.begin(), toupper); // Search for first name found = -1; for(count = 0; count < numRec; count++) { len = name[count].find(",", 0); tempName.assign(name[count], len + 2, name[count].size() - len); transform(tempName.begin(), tempName.end(), tempName.begin(), toupper); len = name[count].size(); if(tempName.compare(0, len, search) == 0) found = count; } break; case '4': // Prompt for full name cout << endl; cout << " Enter name to search for with the format " << endl; cout << endl; cout << "Name to search for: "; cin.ignore(1); getline(cin, search); transform(search.begin(), search.end(), search.begin(), toupper); // Search for full name found = -1; for(count = 0; count < numRec; count++) { len = name[count].size(); transform(tempName.begin(), tempName.end(), tempName.begin(), toupper); if(name[count].compare(0, len, search) == 0) found = count; } break; } // Return and end of menu function return found; } // ************ initializer function ************* int initializer(string name[], string address[], string city[]) { // Declaring variables int count, numRec; ifstream inFile; inFile.open("A:address.txt"); // Check if file is open if(!inFile.is_open()) { cout << "File not found!\n"; exit(0); } // Read in the number of records inFile >> numRec; inFile.ignore(1); // Check for invalid number of records if (numRec > 20) { numRec = 20; cout << "Too many records in file!\nSome files will be lost!\n"; system("pause"); } // Read in all the data from address.txt for(count = 0; count < numRec && count < 20; count++) { getline(inFile, name[count], '#'); getline(inFile, address[count], '#'); getline(inFile, city[count]); // Check for errors in file if ((city[count].compare(0, city[count].size(), "")) == 0) { count = numRec; cout << "Error in file!\nNumber of records invalid\n\n"; exit(0); } // Check for errors in file if ((count != numRec -1) && inFile.eof()) { count = numRec; cout << "Error in file!\nNumber of records invalid\n\n"; exit(0); } } // Close file inFile.close(); // Return and end of initializer function return numRec; } //Fall2001 |
C++ Programming - Address Book Code
Tags
C++ Programming
Social: