aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWiserJ <[email protected]>2021-12-08 17:34:02 -0800
committerWiserJ <[email protected]>2021-12-08 17:34:02 -0800
commitf3627fe5455f5e2c4ddc5d7b953d04bd414af82b (patch)
treeb36ea1dbf5b315919a18c7b0b97322cdf2ad8000
parenth (diff)
downloadcst116-lab9-jeffwoit-f3627fe5455f5e2c4ddc5d7b953d04bd414af82b.tar.xz
cst116-lab9-jeffwoit-f3627fe5455f5e2c4ddc5d7b953d04bd414af82b.zip
crash
-rw-r--r--CST116F2021-Lab9/CST116F2021-Lab9.cpp248
-rw-r--r--CST116F2021-Lab9/CST116F2021-Lab9.vcxproj1
-rw-r--r--CST116F2021-Lab9/CST116F2021-Lab9.vcxproj.filters1
3 files changed, 162 insertions, 88 deletions
diff --git a/CST116F2021-Lab9/CST116F2021-Lab9.cpp b/CST116F2021-Lab9/CST116F2021-Lab9.cpp
index 3499469..f0034dc 100644
--- a/CST116F2021-Lab9/CST116F2021-Lab9.cpp
+++ b/CST116F2021-Lab9/CST116F2021-Lab9.cpp
@@ -14,56 +14,62 @@ const int NAME_SIZE = 100;
int menuSelect(int);
void getInput(char[NAME_SIZE]);
-void readFile(char[NAME_SIZE], string[][ARRAY_SIZE]);
-void findData(string[][ARRAY_SIZE]);
-void addData(string[][ARRAY_SIZE]);
-void sortData(string[][ARRAY_SIZE]);
-void editData(string[][ARRAY_SIZE]);
-void dispData(string[][ARRAY_SIZE]);
+void readFile(char[NAME_SIZE], string[][ARRAY_SIZE], int&);
+void findData(string[][ARRAY_SIZE], int&, int);
+void addData(string[][ARRAY_SIZE], int&);
+void sortData(string[][ARRAY_SIZE], int);
+void editData(string[][ARRAY_SIZE], int, int);
+void dispData(string[][ARRAY_SIZE], int);
+void writeFile(string[][ARRAY_SIZE], int, char[NAME_SIZE]);
int main()
{
- int menu = 0;
+ int menu = 0, loc = 0, size = 0;
char file_name[NAME_SIZE]{};
string info[NUM_RECORDS][ARRAY_SIZE];
getInput(file_name);
- readFile(file_name);
+ readFile(file_name, info, size);
while (menu != 5)
- menuSelect(menu);
-
- switch (menu)
- {
- default:
- {
- cout << "Invalid menu selection." << endl;
- break;
- }
- case 1:
- {
- findData(info);
- break;
- }
- case 2:
{
- addData(info);
- break;
- }
- case 3:
- {
- editData(info);
- break;
- }
- case 4:
- {
- dispData(info);
- break;
- }
- case 5:
- break;
+ menu = menuSelect(menu);
+
+ switch (menu)
+ {
+ default:
+ {
+ cout << "Invalid menu selection." << endl;
+ break;
+ }
+ case 1:
+ {
+ findData(info, loc, size);
+ break;
+ }
+ case 2:
+ {
+ addData(info, size);
+ break;
+ }
+ case 3:
+ {
+ editData(info, loc, size);
+ break;
+ }
+ case 4:
+ {
+ dispData(info, size);
+ break;
+ }
+ case 5:
+ {
+ writeFile(info, size, file_name);
+ break;
+ }
+ }
}
- //Before program closes, files must be updated with the new database stored in 'info'
+
cout << "Exiting Program." << endl;
return 0;
}
@@ -74,29 +80,33 @@ void getInput(char inputFileName[])
cin >> inputFileName;
}
-void readFile(char readFileName[], string readArray[][ARRAY_SIZE])
+void readFile(char readFileName[], string readArray[][ARRAY_SIZE], int& readSize)
{
int i = 0, j = 0;
- string temp;
ifstream inFile;
inFile.open(readFileName);
+
if (inFile.is_open())
{
cout << "File is opened." << endl;
- while (!inFile.eof())
+ while (!inFile.eof() && i < NUM_RECORDS)
{
- //add a counter i for the number of lines in the inFile
while (j < ARRAY_SIZE)
{
- getline(inFile, temp, ' ');
- readArray[i][j] = temp;
+ if (j == 3)
+ getline(inFile, readArray[i][j], '\n');
+ else
+ getline(inFile, readArray[i][j], ' ');
j++;
}
+ j = 0;
+ i++;
}
+ readSize = i;
}
- inFile.close(); //going to be editting the inFile so don't close yet
+ inFile.close();
return;
}
@@ -108,60 +118,98 @@ int menuSelect(int inputMenu)
return inputMenu;
}
-void findData(string findArray[][ARRAY_SIZE])
+void findData(string findArray[][ARRAY_SIZE], int& findLoc, int findSize)
{
- string name;
- int found = false, left = 0, right = sizeof(findArray), mid = 0;
-
+ int i = 0;
+ int found = false;
+ string name = {};
+
cout << "Please enter the last name of the user you are looking for: ";
cin >> name;
- while (!found && left <= right)
- {
+
+ while (i < findSize && found == 0)
+ {
+ if (name.compare(findArray[i][0]) == 0)
+ {
+ found = true;
+ findLoc = i;
+ }
+ i++;
+ }
+ if (!found)
+ cout << "Entry does not exist in database." << endl;
+ else
+ {
+ for (int j = 0; j < ARRAY_SIZE; j++)
+ cout << findArray[findLoc][j] << ' ';
+ cout << endl;
}
- //Find function needs to specify the location of the user for edit function
}
-void addData(string addArray[][ARRAY_SIZE])
+void addData(string addArray[][ARRAY_SIZE], int& addSize)
{
- int i = 0;
+ int i = addSize;
+
+ cout << "Please enter the last name: ";
+ cin >> addArray[i][0];
- while (i < NUM_RECORDS)
- {
- if (addArray[i][0] == "\0") //Should stop at the first empty entry
- {
- cout << "Please enter the last name: ";
- cin >> addArray[i][0];
+ cout << "Please enter the first name: ";
+ cin >> addArray[i][1];
- cout << "Please enter the first name: ";
- cin >> addArray[i][1];
+ cout << "Please enter the phone number in format ###-###-####: ";
+ cin >> addArray[i][2];
- cout << "Please enter the phone number in format ###-###-####: ";
- cin >> addArray[i][2];
+ cout << "Please enter the birthday in format mm-dd-yyyy: ";
+ cin >> addArray[i][3];
- cout << "Please enter the birthday in format mm-dd-yyyy: ";
- cin >> addArray[i][3];
- break;
- }
- else
- i++;
- }
- sortData(addArray);
+ addSize += 1;
+ sortData(addArray, addSize);
}
-void sortData(string sortArray[][ARRAY_SIZE])
+void sortData(string sortArray[][ARRAY_SIZE], int sortSize)
{
-
+ int i = 0, j = 1, k = 0, l = 0;
+ string temp;
+ char first1 = {}, first2 = {};
+ while (l < sortSize)
+ {
+ while (i < sortSize - 1)
+ {
+ if (sortArray[l][0] != "\0")
+ {
+ first1 = sortArray[i][0].front();
+ if (sortArray[j][0] != "\0")
+ {
+ first2 = sortArray[j][0].front();
+ if (first1 > first2)
+ {
+ while (k < ARRAY_SIZE)
+ {
+ temp = sortArray[i][k];
+ sortArray[i][k] = sortArray[j][k];
+ sortArray[j][k] = temp;
+ k++;
+ }
+ k = 0;
+ }
+ }
+ }
+ i++;
+ j++;
+ }
+ i = 0;
+ j = 1;
+ l++;
+ }
}
-void editData(string editArray[][ARRAY_SIZE])
+void editData(string editArray[][ARRAY_SIZE], int editLoc, int editSize)
{
- int select = 0, i = 0;
- findData(editArray);
- //Find function needs to give a value for i so the program correctly edits the specified user
-
+ int select = 0;
+ findData(editArray, editLoc, editSize);
cout << "Please select from the following options:" << endl << "\t1) Change Last Name" << endl << "\t2) Change First Name" << endl << "\t3) Change Phone Number" << endl << "\t4) Change Birthday" << endl << "\t5) Quit" << endl;
cin >> select;
@@ -176,25 +224,25 @@ void editData(string editArray[][ARRAY_SIZE])
case 1:
{
cout << "Please enter the last name: ";
- cin >> editArray[i][0];
+ cin >> editArray[editLoc][0];
break;
}
case 2:
{
cout << "Please enter the first name: ";
- cin >> editArray[i][1];
+ cin >> editArray[editLoc][1];
break;
}
case 3:
{
cout << "Please enter the phone number in format ###-###-####: ";
- cin >> editArray[i][2];
+ cin >> editArray[editLoc][2];
break;
}
case 4:
{
cout << "Please enter the birthday in format mm-dd-yyyy: ";
- cin >> editArray[i][3];
+ cin >> editArray[editLoc][3];
break;
}
case 5:
@@ -204,15 +252,15 @@ void editData(string editArray[][ARRAY_SIZE])
}
}
if (select > 0 && select <= 4)
- sortData(editArray);
+ sortData(editArray, editSize);
}
-void dispData(string dispArray[][ARRAY_SIZE])
+void dispData(string dispArray[][ARRAY_SIZE], int dispSize)
{
cout << setw(20) << "Entry Number" << setw(20) << "Last Name" << setw(20) << "First Name" << setw(20) << "Phone Number" << setw(20) << "Birthday" << endl;
- for (int i = 0; i < sizeof(dispArray); i++) //Should only display filled entries
+ for (int i = 0; i < dispSize; i++)
{
- cout << setw(20) << i;
+ cout << setw(20) << i + 1;
for (int j = 0; j < ARRAY_SIZE; j++)
{
cout << setw(20) << dispArray[i][j];
@@ -221,6 +269,30 @@ void dispData(string dispArray[][ARRAY_SIZE])
}
}
+void writeFile(string writeArray[][ARRAY_SIZE], int writeSize, char writeName[NAME_SIZE])
+{
+ int i = 0, j = 0;
+ ofstream outFile;
+
+ outFile.open(writeName);
+
+ if (outFile.is_open())
+ {
+ while (i < writeSize)
+ {
+ while (j < ARRAY_SIZE)
+ {
+ if(j != 3)
+ outFile << left << setw(sizeof(writeArray[i][j]) + 1) << writeArray[i][j] << ' ';
+ else
+ outFile << left << setw(sizeof(writeArray[i][j])+ 1) << writeArray[i][j] << endl;
+ }
+ }
+ }
+
+ outFile.close();
+}
+
//PROGRAM 2
//#include <iostream>
diff --git a/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj b/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj
index 97d7b76..0e15de8 100644
--- a/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj
+++ b/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj
@@ -142,6 +142,7 @@
<ClCompile Include="CST116F2021-Lab9.cpp" />
</ItemGroup>
<ItemGroup>
+ <Text Include="..\..\..\..\..\..\TEMP\337_Data.txt" />
<Text Include="..\337_Integers.txt" />
<Text Include="..\337_Sentences.txt" />
<Text Include="..\Runs.txt" />
diff --git a/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj.filters b/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj.filters
index 6d88e60..b94ffdf 100644
--- a/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj.filters
+++ b/CST116F2021-Lab9/CST116F2021-Lab9.vcxproj.filters
@@ -23,5 +23,6 @@
<Text Include="..\337_Integers.txt" />
<Text Include="..\Runs.txt" />
<Text Include="..\337_Sentences.txt" />
+ <Text Include="..\..\..\..\..\..\TEMP\337_Data.txt" />
</ItemGroup>
</Project> \ No newline at end of file