aboutsummaryrefslogtreecommitdiff
path: root/16a4
diff options
context:
space:
mode:
Diffstat (limited to '16a4')
-rw-r--r--16a4/16a4/16a4.cpp330
-rw-r--r--16a4/16a4/16a4.h29
-rw-r--r--16a4/16a4/16a4.vcxproj3
-rw-r--r--16a4/16a4/16a4.vcxproj.filters5
-rw-r--r--16a4/16a4/output.txt83
-rw-r--r--16a4/database.txt4
6 files changed, 444 insertions, 10 deletions
diff --git a/16a4/16a4/16a4.cpp b/16a4/16a4/16a4.cpp
index 7502759..c264248 100644
--- a/16a4/16a4/16a4.cpp
+++ b/16a4/16a4/16a4.cpp
@@ -2,19 +2,329 @@
//
#include <iostream>
+#include <string>
+#include <algorithm>
+#include <iomanip>
+//#include "16a4.h"
+
+using namespace std;
+
+const int FN = 0, LN = 1, PN = 2, BD = 3, DATA_POINTS = 4;
+const int MAX_SIZE = 100;
+
+const char FILE_NAME[] = "C:\\Users\\jakno\\source\\repos\\cst116-lab9-JacobAKnox\\16a4\\database.txt";
+
+void menu(int choice, string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void exit(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void findPerson(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void addPerson(string info[MAX_SIZE][DATA_POINTS], int& entries);
+
+void editPerson(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void display(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+string readString(const std::string& question, const std::string& error);
+
+int readInfoUser(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+int readDatabase(string info[MAX_SIZE][DATA_POINTS]);
+
+void writeDatabse(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void sort(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void swap(string info[MAX_SIZE][DATA_POINTS], int a, int b);
+
+void outputEntry(string info[MAX_SIZE][DATA_POINTS], int i);
+
+int binarySearch(string info[MAX_SIZE][DATA_POINTS], int l, int r, string x);
+
+int readInt(const std::string& question, const std::string& error);
+
+void outputData(string info[MAX_SIZE][DATA_POINTS], int i);
int main()
{
- std::cout << "Hello World!\n";
+ string info[MAX_SIZE][DATA_POINTS];
+ int entries = 0, choice = 0;
+
+ entries = readDatabase(info);
+ menu(choice, info, entries);
+
+}
+
+void menu(int choice, string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ string menu = "\t\t--Database program--\n\
+1. Find a person's information\n\
+2. Add a person to the database\n\
+3. Edit a person's information\n\
+4. Display all records to the screen\n\
+5. Exit Program\n\n\
+Input your choice: ";
+
+ while (choice != 5)
+ {
+ choice = readInt(menu, "Error please input an integer 1-5.\n");
+
+ switch (choice)
+ {
+ case 1:
+ findPerson(info, entries);
+ break;
+ case 2:
+ addPerson(info, entries);
+ break;
+ case 3:
+ editPerson(info, entries);
+ break;
+ case 4:
+ display(info, entries);
+ break;
+ case 5:
+ exit(info, entries);
+ return;
+ default:
+ cout << "Error unknown choice " << choice << ". Please try again.";
+ break;
+ }
+ }
}
-// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
-// Debug program: F5 or Debug > Start Debugging menu
+void exit(string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ cout << "Saving database...\n";
+ writeDatabse(info, entries);
+ cout << "Exiting...";
+}
+
+void findPerson(string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ string name = readString("Input the last name to search for: ", "Could not read name. Please try again.");
+
+ int loc = binarySearch(info, 0, entries - 1, name);
+ if (loc == -1)
+ {
+ cout << "Could not find entry with the last name " << name << ".\n";
+ }
+ else
+ {
+ outputEntry(info, loc);
+ }
+}
+
+void addPerson(string info[MAX_SIZE][DATA_POINTS], int& entries)
+{
+ entries = readInfoUser(info, entries);
+ sort(info, entries);
+}
+
+void editPerson(string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ string name = readString("Input the last name to search for: ", "Could not read name. Please try again.");
+
+ int loc = binarySearch(info, 0, entries - 1, name);
+ if (loc == -1)
+ {
+ cout << "Could not find entry with the last name " << name << ".\n";
+ return;
+ }
+
+ string first, last, phone, birthday;
+
+ first = readString("Input the new first name: ", "Could not read first name. Try again.\n");
+ last = readString("Input the new last name: ", "Could not read last name. Try again.\n");
+ phone = readString("Input the new phone number: ", "Could not read phone number. Try again.\n");
+ birthday = readString("Input the new birthday: ", "Could not read birthday. Try again.\n");
+
+ info[loc][LN] = last;
+ info[loc][FN] = first;
+ info[loc][PN] = phone;
+ info[loc][BD] = birthday;
+
+ sort(info, entries);
+
+ cout << "Entry updated.\n";
+}
+
+void display(string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ cout << setw(15) << left << "First Name: " << setw(15) << left << "Last Name: " << setw(15) << left << "Phone Number: "
+ << setw(15) << left << "Birthday: " << endl;
+
+ for (int i = 0; i < entries; i++)
+ {
+ outputData(info, i);
+ }
+}
+
+int readInt(const std::string& question, const std::string& error)
+{
+ int value;
+
+ cout << question;
+ while (!(cin >> value))
+ {
+ cin.clear();
+ while (cin.get() != '\n') continue;
+ cout << error;
+ cout << question;
+ }
+ cin.ignore();
+ return value;
+}
+
+string readString(const std::string& question, const std::string& error)
+{
+ string value;
+
+ cout << question;
+ getline(cin, value);
+
+ return value;
+}
+
+int readInfoUser(string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ string first, last, phone, birthday;
+
+ first = readString("Input the first name: ", "Could not read first name. Try again.\n");
+ last = readString("Input the last name: ", "Could not read last name. Try again.\n");
+ phone = readString("Input the phone number: ", "Could not read phone number. Try again.\n");
+ birthday = readString("Input the birthday: ", "Could not read birthday. Try again.\n");
+
+ info[entries][LN] = last;
+ info[entries][FN] = first;
+ info[entries][PN] = phone;
+ info[entries][BD] = birthday;
+
+ return ++entries;
+
+}
+
+int readDatabase(string info[MAX_SIZE][DATA_POINTS])
+{
+ FILE* file;
+ char temp[100];
+ int entries = 0;
+
+ int err = fopen_s(&file, FILE_NAME, "r");
+
+ if (!err && file != NULL)
+ {
+ while (!feof(file)) {
+ fscanf_s(file, "%s ", temp, sizeof(temp));
+ if (feof(file))
+ {
+ fclose(file);
+ return entries;
+ }
+ strcat_s(temp, "\0");
+ string s = string(temp);
+ info[entries][FN] = s;
+
+ fscanf_s(file, "%s ", temp, sizeof(temp));
+ strcat_s(temp, "\0");
+ s = string(temp);
+ info[entries][LN] = s;
+
+ fscanf_s(file, "%s ", temp, sizeof(temp));
+ strcat_s(temp, "\0");
+ s = string(temp);
+ info[entries][PN] = s;
+
+ fscanf_s(file, "%s ", temp, sizeof(temp));
+ strcat_s(temp, "\0");
+ s = string(temp);
+ info[entries][BD] = s;
+
+ entries++;
+ }
+
+ fclose(file);
+ }
+
+ return entries;
+
+}
+
+void writeDatabse(string info[MAX_SIZE][DATA_POINTS], int entries)//todo test this program
+{
+ FILE* file;
+
+ sort(info, entries);
+
+ fopen_s(&file, FILE_NAME, "w");
+
+ for (int i = 0; i < entries; i++)
+ {
+ string line = "";
+ for (int j = 0; j < DATA_POINTS; j++)
+ {
+ line += info[i][j] + " ";
+ }
+ line += "\n";
+ fwrite(line.c_str(), sizeof(char), line.length(), file);
+ }
+
+ fclose(file);
+}
+
+void sort(string info[MAX_SIZE][DATA_POINTS], int entries)
+{
+ for (int i = 0; i < entries; i++)
+ {
+ for (int j = 0; j < entries - i - 1; j++)
+ {
+ if (info[j][LN] > info[j + 1][LN])
+ {
+ swap(info, j, j + 1);
+ }
+ }
+ }
+}
+
+void swap(string info[MAX_SIZE][DATA_POINTS], int a, int b)
+{
+ string temp[DATA_POINTS];
+
+ copy(info[a], info[a] + DATA_POINTS, temp);
+ copy(info[b], info[b] + DATA_POINTS, info[a]);
+ copy(temp, temp + DATA_POINTS, info[b]);
+}
+
+void outputEntry(string info[MAX_SIZE][DATA_POINTS], int i)
+{
+ cout << setw(12) << "First Name: " << setw(15) << left << info[i][FN] << "Last Name: " << setw(15) << left << info[i][LN]
+ << setw(14) << "Phone Number: " << setw(10) << left << info[i][PN] << setw(10) << "Birthday: " << setw(10) << left << info[i][BD] << endl;
+}
+
+void outputData(string info[MAX_SIZE][DATA_POINTS], int i)
+{
+ cout << setw(15) << left << info[i][FN] << setw(15) << left << info[i][LN] << setw(15) << left << info[i][PN] << setw(15) << left << info[i][BD] << endl;
+}
+
+int binarySearch(string info[MAX_SIZE][DATA_POINTS], int l, int r, string x)
+{
+ if (r >= l)
+ {
+ int mid = l + (r - l) / 2;
+
+ if (info[mid][LN] == x)
+ {
+ return mid;
+ }
+
+ if (info[mid][LN] > x)
+ {
+ return binarySearch(info, l, mid - 1, x);
+ }
+
+ return binarySearch(info, mid + 1, r, x);
+ }
-// 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
+ return -1;
+} \ No newline at end of file
diff --git a/16a4/16a4/16a4.h b/16a4/16a4/16a4.h
new file mode 100644
index 0000000..fd0d176
--- /dev/null
+++ b/16a4/16a4/16a4.h
@@ -0,0 +1,29 @@
+#pragma once
+
+void menu(int choice, string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void exit(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void findPerson(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void addPerson(string info[MAX_SIZE][DATA_POINTS], int& entries);
+
+void editPerson(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void display(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+string readString(const std::string& question, const std::string& error);
+
+int readInfoUser(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+int readDatabase(string info[MAX_SIZE][DATA_POINTS]);
+
+void writeDatabse(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void sort(string info[MAX_SIZE][DATA_POINTS], int entries);
+
+void swap(string info[MAX_SIZE][DATA_POINTS], int a, int b);
+
+void outputEntry(string info[MAX_SIZE][DATA_POINTS], int i);
+
+int binarySearch(string info[MAX_SIZE][DATA_POINTS], int l, int r, string x);
diff --git a/16a4/16a4/16a4.vcxproj b/16a4/16a4/16a4.vcxproj
index a960269..acfe662 100644
--- a/16a4/16a4/16a4.vcxproj
+++ b/16a4/16a4/16a4.vcxproj
@@ -141,6 +141,9 @@
<ItemGroup>
<ClCompile Include="16a4.cpp" />
</ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="16a4.h" />
+ </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
diff --git a/16a4/16a4/16a4.vcxproj.filters b/16a4/16a4/16a4.vcxproj.filters
index e1d5847..a3ef33f 100644
--- a/16a4/16a4/16a4.vcxproj.filters
+++ b/16a4/16a4/16a4.vcxproj.filters
@@ -19,4 +19,9 @@
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="16a4.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ </ItemGroup>
</Project> \ No newline at end of file
diff --git a/16a4/16a4/output.txt b/16a4/16a4/output.txt
new file mode 100644
index 0000000..c326135
--- /dev/null
+++ b/16a4/16a4/output.txt
@@ -0,0 +1,83 @@
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 4
+First Name: Last Name: Phone Number: Birthday:
+John Apple 1234567890 12/2/2021
+Jim Steve 7544787854 3/6/1436
+Steve Test 761432798 3/23/1423
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 2
+Input the first name: Bob
+Input the last name: Hello
+Input the phone number: 768467789
+Input the birthday: 5/21/1673
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 1
+Input the last name to search for: Apple
+First Name: John Last Name: Apple Phone Number: 1234567890Birthday: 12/2/2021
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 1
+Input the last name to search for: Steve
+First Name: Jim Last Name: Steve Phone Number: 7544787854Birthday: 3/6/1436
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 3
+Input the last name to search for: Apple
+Input the new first name: John
+Input the new last name: Zebra
+Input the new phone number: 1234567890
+Input the new birthday: 12/2/2021
+Entry updated.
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 4
+First Name: Last Name: Phone Number: Birthday:
+Bob Hello 768467789 5/21/1673
+Jim Steve 7544787854 3/6/1436
+Steve Test 761432798 3/23/1423
+John Zebra 1234567890 12/2/2021
+ --Database program--
+1. Find a person's information
+2. Add a person to the database
+3. Edit a person's information
+4. Display all records to the screen
+5. Exit Program
+
+Input your choice: 5
+Saving database...
+Exiting...
+C:\Users\jakno\source\repos\cst116-lab9-JacobAKnox\16a4\16a4\Debug\16a4.exe (process 15276) exited with code 0.
+Press any key to close this window . . .
diff --git a/16a4/database.txt b/16a4/database.txt
new file mode 100644
index 0000000..cf9e95a
--- /dev/null
+++ b/16a4/database.txt
@@ -0,0 +1,4 @@
+Bob Hello 768467789 5/21/1673
+Jim Steve 7544787854 3/6/1436
+Steve Test 761432798 3/23/1423
+John Zebra 1234567890 12/2/2021