// NAME: Page Lynn Potter // E-MAIL: ppotter3@cnm.edu or ppotter03@inbox.com // FILE: P1_NameSearch_Functions.cpp // This program will cover strings, arrays, file I/O, pointers/references. // OBJECTIVE: Read a file of first names and search fro names entered by the user. #include // Needed for cin and cout #include // Needed for string values #include // Reading/Writing file requires this standard library. using namespace std; // For easier access #define is used for file names #define FILE_IN "FirstNames.txt" #define FILE_OUT "FirstNames_Summary.txt" void Objective_screen(string *pName) { // Declared variables char ENTER; // Greeting and introduction to the program // Ask for user name and return string cout << "\n Hello! \n"; cout << "\n Welcome! \n"; cout << "\n THE C++ SEARCH FOR FIRST NAMES PROGRAM \n"; cout << "\n My name is Page, and I will go over the main " << "\n objective and instructions for the program. \n"; cout << "\n If you do not mind I have a question..."; cout << "\n What is your full name? "; getline(cin, *pName); cout << "\n Nice to meet you " << *pName << ". \n"; cout << "\n This program uses input/output data files for the information " << "\n and the array. The array is used for data of the same type " << "\n (variables) that is best grouped together. \n"; cout << "\n After the data file has been accessed the names will be sorted " << "\n as specified later in the program. \n"; cout << "\n LET'S START! Hit ENTER to continue... \n"; // Waits for user's input before program continues cin.get(ENTER); } bool ReadFile(string names[], int &rTotal) { /* This is the function that will fill the name[] array, after the data file has been successfully located.*/ // Declared Variables string filename = "FirstNames.txt"; /* Uses the 'open' member function of the ifstream object to open a file for reading. */ ifstream FirstNamesInput; /* Since the file name is a string the 'open' element needs a char[]. So, it is nessecary to us the c_str() function to give the string in proper char[] format. */ FirstNamesInput.open(filename.c_str()); // Here we must make sure that the file was opened if(!FirstNamesInput) { cout << "\n Opps! ERROR... Can't Find " << filename; cout << "\n =============================================== \n " << endl; exit(1); // Returns the false value (or failure) in boolean form return false; } /* Needed to make sure I count the words/names in the .txt file. The *pIndex is used as a reference parameter to located the total number of strings in the file as well as the first element. */ while(FirstNamesInput >> names[rTotal]) rTotal++; cout << "\n There are a total of " << rTotal << " names in the file. " << endl; // Collecting data for the array and filling the names into the array. for(int i = 1; i < rTotal + 1; ++i) { FirstNamesInput >> names[i-1]; } // Returns the true value (or success) in boolean form return true; } void bubbleSort(string names[], int &rTotal) { // Declared Variables // This is used for a place holder of a temporary string (name). string temp; /* This loop compares the availible adjacent values, and moves the values around untill they are in the correct order. In other word... it will help to properly sort the names in this situation. */ // This is the first of dual (nested) loop for(int i = 0; i < rTotal - 1; ++i) { bool swapped = false; // This is the second of dual (nested) loop for(int j = 1; j < rTotal; ++j) { /* This is used to determine if the specified array element [j - 1] is larger than [j]. */ if(names[j - 1] > names[j]) { // Temp holds value of [j - 1] temp = names[j - 1]; // Array element is assigned the value of [j - 1] names[j - 1] = names[j]; // Element [j - 1] is replaced by temp names[j] = temp; } } } } void AskForTwoNames(string &rUserInput) { // This function will ask the user to input two first names of thier choice. cout << "\n ENTER two first names, In CAPS, seperated by a SPACE: " << endl; getline(cin, rUserInput); cin.ignore(); // Makes sure that the user enters two names and not nothing. //if(rUserInput.length() == 0) //{ // cout << "\n Please enter in two first names, again: " << endl; // getline(cin, rUserInput); //} } void SearchNames(string names[], int &rTotal, string &rUserInput) { cout << "\n TESTING FUNCTIONS! "; } bool WriteOutput(string *name, string names[], int &rTotal, string &rUserInput) { // Declared Variables string filename = "FirstNames_Summary.txt"; /* The WriteOutput() function recieves the values that were passed to them as well as the users input for the search and displays them to the screen. This file also shows the user the output file for the results and or summary of the outcome of the program. */ // This will make FirstNamesOutput cast as cin or cout ofstream FirstNamesOutput; // Needed to open output file FirstNamesOutput.open(filename.c_str()); // This is used to test if the specified output file exists if(!FirstNamesOutput) { // Let user know if there was an error and/or no output file was found cout << "\n Opps! ERROR... Can't Find " << filename; cout << "\n =============================================== \n " << endl; exit(1); // Returns the false value (or failure) in boolean form return false; } else { // Let user know that the program has successfully found and identified output file cout << "\n CONGRATULATIONS! C++ SEARCH FOR FIRST NAMES PROGRAM FOUND " << "\n THE OUTPUT FILE CALLED: ------!> " << FILE_OUT << "\n"; FirstNamesOutput << "\n ----------!> RESULTS