diff options
| author | tylr <[email protected]> | 2021-12-06 12:38:44 -0800 |
|---|---|---|
| committer | tylr <[email protected]> | 2021-12-06 12:38:44 -0800 |
| commit | 5c2fdc0add9db89799eab043ede16d2be20f4d25 (patch) | |
| tree | b87e70992478fb07b2e3e353441db5582e32ddbd | |
| parent | Complete number 3. (diff) | |
| download | cst116-lab9-till-t-5c2fdc0add9db89799eab043ede16d2be20f4d25.tar.xz cst116-lab9-till-t-5c2fdc0add9db89799eab043ede16d2be20f4d25.zip | |
Num 4 in progress.
| -rw-r--r-- | num3/data2.txt | 2 | ||||
| -rw-r--r-- | num3/hello.txt | 1 | ||||
| -rw-r--r-- | num3/num3.cpp | 9 | ||||
| -rwxr-xr-x | num4/a.out | bin | 0 -> 77314 bytes | |||
| -rw-r--r-- | num4/data.txt | 3 | ||||
| -rw-r--r-- | num4/data_copy.txt | 3 | ||||
| -rw-r--r-- | num4/main.cpp | 298 |
7 files changed, 309 insertions, 7 deletions
diff --git a/num3/data2.txt b/num3/data2.txt index 6f9989e..6b2f590 100644 --- a/num3/data2.txt +++ b/num3/data2.txt @@ -1,4 +1,4 @@ This file was built by Tylr -We included one blank line here? +We included one blank line above Lets see if this works! diff --git a/num3/hello.txt b/num3/hello.txt index 3870db1..572f051 100644 --- a/num3/hello.txt +++ b/num3/hello.txt @@ -2,4 +2,3 @@ Hello! Everything must end in a period for this to work. No period Let's see - diff --git a/num3/num3.cpp b/num3/num3.cpp index d3ea934..16ac4a4 100644 --- a/num3/num3.cpp +++ b/num3/num3.cpp @@ -3,7 +3,6 @@ //Dec 3, 2021 - #include <iostream> #include <fstream> #include <stdio.h> @@ -31,9 +30,9 @@ int main() int record_counter = 0; //arrays - std::string data[MAX]; - int length_arr[MAX]; - int charCount_arr[MAX]; + std::string data[MAX]; //holds strings. + int length_arr[MAX]; //keeps track of full length of strings. Counts characters including spaces. + int charCount_arr[MAX]; // //display the file choices @@ -69,7 +68,7 @@ int main() flag = 0; } - //Builds arrays. See functions below for descriptions. + //Build arrays. See functions below for descriptions. FindLength(data, length_arr); CharCount(data, length_arr, charCount_arr); diff --git a/num4/a.out b/num4/a.out Binary files differnew file mode 100755 index 0000000..b308005 --- /dev/null +++ b/num4/a.out diff --git a/num4/data.txt b/num4/data.txt new file mode 100644 index 0000000..157d902 --- /dev/null +++ b/num4/data.txt @@ -0,0 +1,3 @@ +tyler taormina 714-808-2056 05/17/1994 +taury hlinka 714-474-6185 07/21/1995 +ryan taormina 714-555-5555 03/31/1996 diff --git a/num4/data_copy.txt b/num4/data_copy.txt new file mode 100644 index 0000000..157d902 --- /dev/null +++ b/num4/data_copy.txt @@ -0,0 +1,3 @@ +tyler taormina 714-808-2056 05/17/1994 +taury hlinka 714-474-6185 07/21/1995 +ryan taormina 714-555-5555 03/31/1996 diff --git a/num4/main.cpp b/num4/main.cpp new file mode 100644 index 0000000..00b63bc --- /dev/null +++ b/num4/main.cpp @@ -0,0 +1,298 @@ +// tyler taormina +// cst 116 +// lab 7 +// number 1 + +#include <iostream> +#include <fstream> +#include <algorithm> +#include <stdio.h> +#include <iomanip> +#include <string> + +using namespace std; +const int MAX = 10; + +void RmString(string arr[], int); +void ClearBuffer(); +void DisplayMenu(int&); +void ProcessMenuChoice (int&, int&, int&, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]); + +void AddPerson(int, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]); +void PrintData(int, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]); +void FindString (string first[], string last[], string phone[], string bday[], int); +int ReadData (ifstream & inFile, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]); + +void OutData(ofstream & outFile, string first[], string last[], string phone[], string bday[], int record_counter ); + +int main() +{ + std::string f_name[MAX]; + std::string l_name[MAX]; + std::string phone[MAX]; + std::string bday[MAX]; + + + int record_counter = 0; + int menu_choice; + int flag = 1; + + ifstream inFile; + + //open file and read data + + inFile.open ( "data.txt"); + + if ( inFile.is_open ( ) ) + { + record_counter = ReadData (inFile, f_name, l_name, phone, bday); + inFile.close ( ); + } + + else + { + cout << "Trouble Opening File: inFile"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + + //User interacts with program here. + while (flag != 0) + { + DisplayMenu(menu_choice); + ProcessMenuChoice(menu_choice, flag, record_counter, f_name, l_name, phone, bday); + } + //Writes changes to the data.txt file + ofstream outFile ( "data.txt" ); + if ( outFile.is_open ( ) ) + { + + OutData(outFile, f_name, l_name, phone, bday, record_counter); + outFile.close ( ); + } + else + { + cout << "Trouble Opening File: outFile"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + + return 0; +} + + +int ReadData ( ifstream & inFile, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]) +{ + int counter = 0; + inFile >> first[counter] >> last[counter] >> phone[counter] >> bday[counter]; // Priming Read + + while ( !inFile.eof ( ) ) + { + counter++; + inFile >> first[counter] >> last[counter] >> phone[counter] >> bday[counter]; + } + + return counter; +} + + +/* void FindString(string first[], int limit) */ +/* { */ +/* // Check for a substring inside of one of the input strings. */ +/* // Prints whether or not the string was found. If found */ +/* // prints the index of the list where the string was found */ +/* // and what the string was that contained the substring. */ + +/* string sub; */ +/* int i, j; */ +/* int reset = 0; */ +/* ClearBuffer(); */ + +/* cout << "Lets look for a substring..." << endl; */ +/* cout << "Please enter a substring to look for: \n" << endl; */ +/* getline(cin, sub); */ + +/* for (i = 0; i < limit; i++) { */ +/* if (arr[i].size() < sub.size()) */ +/* i = limit; */ +/* for (j = 0; j < arr[i].size(); j++) { */ +/* int k = 0; */ +/* if (arr[i][j] == sub[k]) { */ +/* reset = j; */ +/* while (arr[i][j] == sub[k] && k < sub.size()) { */ +/* j++; */ +/* k++; */ +/* } */ + +/* if (k == sub.size()){ */ +/* cout << "We found it at index: " << i+1 << endl; */ +/* cout << "The string was: " << arr[i] << endl; */ +/* } */ +/* else */ +/* j = reset; */ +/* } */ +/* } */ +/* } */ +/* } */ + + +void PrintData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]) +{ + //Displays all strings stored. + int i = 0; + cout << "==================================================================\n"; + cout << "This is what we have in our database so far..." << endl; + cout << "==================================================================\n"; + + while (i < record_counter) + { + cout << i+1 << ") " << first[i] << " " << last[i] << " " << phone[i] << " " << bday[i] << endl; + i += 1; + } +} + + +void AddPerson(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]) +{ + std::string temp; + cout << "Enter the first name of the person you'd like to add: " << endl; + cin >> temp; + first[record_counter] = temp; + cout << "Enter the last name of the person you'd like to add: " << endl; + cin >> temp; + last[record_counter] = temp; + cout << "Enter the phone number of the person you'd like to add: " << endl; + cin >> temp; + phone[record_counter] = temp; + cout << "Enter the birthday of the person you'd like to add: " << endl; + cin >> temp; + bday[record_counter] = temp; +} + +/* void RmString(string arr[], int limit) */ +/* { */ +/* // Finds string within array and removes the element. */ +/* // May need to use vector. */ +/* int i = 0, j = 0, k = 0; */ +/* int choice; */ +/* string deleted; */ +/* string copy[max]; */ + +/* cout << "What data entry would you like to delete..." << endl; */ +/* PrintString(limit, arr); */ +/* cout << "Please enter the number for the string that you'd like to delte: " << endl; */ +/* cin >> choice; */ +/* choice -= 1; */ +/* cout << "Working...\n\n" << endl; */ + +/* for (j = 0 ; j < limit + 1; j++) */ +/* { */ +/* if (k == choice) { */ +/* cout << "Deleting...\n\n" << endl; */ +/* deleted = arr[k]; */ +/* k++; */ +/* j--; */ + +/* } */ +/* else { */ +/* copy[j] = arr[k]; */ +/* k++; */ +/* } */ +/* } */ + +/* cout << "==================================================================\n"; */ +/* cout << "Here is our updated database..." << endl; */ +/* cout << "==================================================================\n"; */ + +/* while (i < (limit - 1)) */ +/* { */ +/* cout << i+1 << ") " << copy[i] << endl; */ +/* i++; */ +/* } */ + +/* cout << "We removed: " << deleted << endl; */ +/* } */ + + +void DisplayMenu(int& menu_choice) +{ + //Displays the menu of functions for the user to choose from. + menu_choice = 0; + cout << "==================================================================\n"; + cout << " MENU" << endl; + cout << "==================================================================\n"; + + cout << "1) Find a person's information.\n"; + cout << "2) Add a new person to the database.\n"; + cout << "3) Edit information for a certain individual.\n"; + cout << "4) Display full database.\n"; + cout << "5) Exit Program.\n\n"; + cout << "Enter: "; + cin >> menu_choice; + if (menu_choice > 5 || menu_choice < 1) + { + cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl; + DisplayMenu(menu_choice); + } +} + + +void ProcessMenuChoice (int& menu_choice, int& flag, int& record_counter, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]) +{ + //Takes in user input for menu choice and calls the appropriate function. + ClearBuffer(); + flag = 0; + switch(menu_choice) + { + case 1: + // FindString(FNAME, LNAME, PHONE, BDAY, limit); + break; + + case 2: + record_counter++; + AddPerson(record_counter, first, last, phone, bday); + break; + + case 3: + //Edit information for a certain person + break; + + + case 4: + PrintData(record_counter, first, last, phone, bday); + break; + + case 5: + cout << "Ending program." << endl; + cout << "Are you sure you want to quit?" << endl; + break; + + default: + break; + } + cout << "Press 0 to close the program.\n" << endl; + cout << "Press any other number to continue the program.\n" << endl; + cin >> flag; +} + + +void OutData(ofstream & outFile, string first[], string last[], string phone[], string bday[], int record_counter ) +{ + int i; + cout << "Writing Changes..." << endl; + + for (i = 0; i < record_counter; i++) + { + outFile << first[i] << " " << last[i] << " " << phone[i] << " " << bday[i] << endl; + } +} + + +void ClearBuffer() +{ + // clears buffer after menu choice so as not to interfere with the following user inputs. + char c; + do { + c = getchar(); + } while (c != '\n' && c != EOF); +} + + |