#include #include #include #include #include #include using namespace std; const int DATA = 100; const int RECORDS = 4; // 11.14 - 2 /* void get_nums(int[], int&); void sort_nums(int[], int); void display_nums(int[], int); int main() { int data[DATA]; int nums; get_nums(data, nums); sort_nums(data, nums); display_nums(data, nums); } void get_nums(int data[], int& nums) { string entered; cout << "Enter the path of the selected file: "; cin >> entered; cout << endl; ifstream inFile; inFile.open(entered); if (inFile.is_open()) { for (int i = 0; i < DATA; i++) { nums = i + 1; inFile >> data[i]; if (inFile.eof()) break; } inFile.close(); } else { cout << "Can't find input file " << entered << endl; } } void sort_nums(int data[], int nums) { bool swapped; int temp; for (int i = 0; i < nums - 1; i++) { swapped = false; for (int j = 0; j < nums - 1; j++) { if (data[j] > data[j + 1]) { temp = data[j + 1]; data[j + 1] = data[j]; data[j] = temp; swapped = true; } } if (swapped == false) break; } } void display_nums(int data[], int nums) { cout << "The smallest number in the file is: " << data[0] << "\n\n" << "The biggest number in the file is: " << data[nums - 1] << "\n\n"; cout << "The sorted file:\n"; for (int i = 0; i < nums; i++) { stringstream temp; char length[DATA]; temp << data[i]; temp >> length; cout << i + 1 << ":\t" << setw(12) << left << data[i] << strlen(length) << endl; } cout << endl; } */ // 11.14 - 3 /* void get_data(string[], int&); void display_data(string[], int); int main() { string data[DATA]; int nums; get_data(data, nums); display_data(data, nums); } void get_data(string data[], int& nums) { string entered; cout << "Enter the path of the selected file: "; cin >> entered; cout << endl; ifstream inFile; inFile.open(entered); if (inFile.is_open()) { for (int i = 0; i < DATA; i++) { nums = i + 1; getline(inFile, data[i], '\n'); if (inFile.eof()) break; } inFile.close(); } else { cout << "Can't find input file " << entered << endl; } } void display_data(string data[], int nums) { cout << "The entered file:\n"; for (int i = 0; i < nums; i++) { cout << i + 1 << ":\t" << data[i] << '\t' << strlen(data[i].c_str()) << endl; } cout << endl; } */ // 11.14 - 4 /* void get_data(string[][RECORDS], int&, bool&); void add_data(string[][RECORDS], int&); void find_data(string[][RECORDS], int); int search_data(string[][RECORDS], int, string); void edit_data(string[][RECORDS], int); void sort_data(string[][RECORDS], int); void display_data(string[][RECORDS], int); void save_data(string[][RECORDS], int); int main() { string data[DATA][RECORDS]; int nums; bool open; get_data(data, nums, open); int choice; if (open == true) { do { sort_data(data, nums); cout << "Menu:\n" << "\t1: Find a person's information\n" << "\t2: Add a person to the database\n" << "\t3: Edit a person's information\n" << "\t4: Display all records to the screen\n" << "\t5: Quit\n\n" << "Enter menu choice: "; cin >> choice; cout << endl; switch (choice) { case 1: find_data(data, nums); break; case 2: add_data(data, nums); break; case 3: edit_data(data, nums); break; case 4: display_data(data, nums); break; default: choice = 5; save_data(data, nums); cout << "Exiting program\n\n"; break; } } while (choice != 5); } return 0; } void get_data(string data[][RECORDS], int& nums, bool& open) { ifstream inFile; inFile.open("C:\\Temp\\Lab_9.txt"); if (inFile.is_open()) { open = true; for (int i = 0; i < DATA; i++) { nums = i + 1; for (int j = 0; j < RECORDS; j++) { getline(inFile, data[i][j], ' '); if (inFile.eof()) { nums--; break; } } if (inFile.eof()) break; } inFile.close(); } else { open = false; cout << "Can't find input file " << endl; } } void add_data(string data[][RECORDS], int& nums) { cout << "Enter the person's first and last names, their phone number, and their birthday\n\n"; cin >> data[nums][0] >> data[nums][1] >> data[nums][2] >> data[nums][3]; nums++; } void find_data(string data[][RECORDS], int nums) { int found; string search; cout << "Enter the person's last name: "; cin.ignore(180, '\n'); cin >> search; found = search_data(data, nums, search); if (found == -1) cout << "Person isn't in database.\n\n"; else { cout << "Records found:\n\n"; for (int i = 0; i < RECORDS; i++) { cout << data[found][i] << '\t'; } cout << endl << endl; } } int search_data(string data[][RECORDS], int nums, string search) { int found = -1; for (int i = 0; i < nums; i++) { if (data[i][1] == search) { found = i; } } return found; } void edit_data(string data[][RECORDS], int nums) { int found; string search; cout << "Enter the person's last name: "; cin.ignore(180, '\n'); cin >> search; found = search_data(data, nums, search); if (found == -1) cout << "Person isn't in database.\n\n"; else { cout << "Enter the correct first and last names, phone number, and birthday\n\n"; cin >> data[found][0] >> data[found][1] >> data[found][2] >> data[found][3]; } } void sort_data(string data[][RECORDS], int nums) { bool swapped; string temp[RECORDS]; for (int i = 0; i < nums - 1; i++) { swapped = false; for (int j = 0; j < nums - 1; j++) { if (data[j][1] > data[j + 1][1]) { for (int k = 0; k < RECORDS; k++) { temp[k]= data[j + 1][k]; data[j + 1][k] = data[j][k]; data[j][k] = temp[k]; } swapped = true; } } if (swapped == false) break; } } void display_data(string data[][RECORDS], int nums) { cout << "Printing data:\n\n"; for (int i = 0; i < nums; i++) { for (int j = 0; j < RECORDS; j++) { cout << data[i][j] << ' '; } cout << endl << endl; } } void save_data(string data[][RECORDS], int nums) { ofstream outFile; outFile.open("C:\\Temp\\Lab_9.txt"); if (outFile.is_open()) { for (int i = 0; i < nums; i++) { for (int j = 0; j < RECORDS; j++) { outFile << data[i][j] << ' '; cout << data[i][j] << ' '; } outFile << endl; cout << endl; } outFile.close(); } else { cout << "Can't find output file" << endl; } } */