diff options
| -rw-r--r-- | CST116F2021-Lab9/CST116F2021-Lab9.cpp | 341 |
1 files changed, 328 insertions, 13 deletions
diff --git a/CST116F2021-Lab9/CST116F2021-Lab9.cpp b/CST116F2021-Lab9/CST116F2021-Lab9.cpp index d7b3cce..af3b2b3 100644 --- a/CST116F2021-Lab9/CST116F2021-Lab9.cpp +++ b/CST116F2021-Lab9/CST116F2021-Lab9.cpp @@ -1,20 +1,335 @@ -// CST116F2021-Lab9.cpp : This file contains the 'main' function. Program execution begins and ends there. -// - #include <iostream> +#include <fstream> +#include <string> +#include <cctype> +#include <iomanip> +#include <sstream> +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() { - std::cout << "Hello World!\n"; + 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"); -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu + 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"); -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file + 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; + } +} +*/
\ No newline at end of file |