Hello! My name is Page, and I am still in the process of becoming a more seasoned programmer. I am trying to get a .txt file with first names in it to be sorted after I read the file and input it to a array from a function. Now I know how to sort integers but I am having a extremely hard time fully understanding how to sort a string array.
I also need some direction with searching the array.... I know its very choppy right now (My code). I just am working on getting somewhat of a rough draft before I start to really debug. If possible can I get an example or direction on how to sort and search for two names, and be able to see how many times the names that the user input were found. I have also attached the .txt I will be working with.
Thank you very much.
// FILE: P1_NameSearch_Functions.cpp (Function File)
// 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 <iostream> // Needed for cin and cout
#include <string> // Needed for string values
#include <fstream> // Reading/Writing from 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 *name)
{
// Declared variables
char ENTER;
// Greeting and introduction to the program
// Ask for user name and return string
cout << "\n Hello! \n";
cout << "\n WELCOME TO THE C++ SEARCH FOR NAMES PROGRAM \n";
cout << "\n My name is Page, and I will go over the main "
<< "\n objective and instructions for the C++ SEARCH "
<< "\n FOR NAMES PROGRAM! \n";
cout << "\n If you do not mind I have a question...";
cout << "\n What is your full name? ";
getline(cin, *name);
cout << "\n Nice to meet you " << *name << ". \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 *pTotal)
{
/* 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. */
else
{
FirstNamesInput >> *pTotal;
cout << "\n There are a total of " << *pTotal << " names in the file. " << endl;
}
// Collecting data for the array and filling the names into the array.
for(int i = 1; i < *pTotal + 1; ++i)
{
FirstNamesInput >> names[i-1];
}
// Returns the true value (or success) in boolean form
return true;
}
void bubbleSort(string names[], int *pTotal)
{
// Declared Variables
// This is used for a place holder of a temporary string (name).
string temp;
/* This loop compares the available adjacent values, and moves the values around
until they are in the correct order. In other word... it will help to properly
sort the names in this situation. */
for(int i = 0; i < *pTotal - 1; ++i) // This is the first of dual (nested) loop
{
bool swapped = false;
for(int j = 1; j < *pTotal; ++j) // This is the second of dual (nested) loop
{
/* This is used to determine if the specified array
element [j - 1] is larger than [j]. */
if(names[j] > names[j + 1])
{
temp = names[j]; // Temporary holds value of [j]
names[j] = names[j - 1]; // Array element is assigned the value of [j - 1]
names[j - 1] = temp; // Element [j - 1] is replaced by temporary
swapped = true;
}
}
if (!swapped) break; // The names are all sorted.
}
}
void AskForTwoNames(string *pUserInput)
{
// This function will ask the user to input two first names of their choice.
cout << "\n Please enter in two first names, in CAPS, separated by a space: " << endl;
getline(cin, *pUserInput);
// Makes sure that the user enters two names and not nothing.
if(*pUserInput.length() == 0)
{
cout << "\n Please enter in two first names, again: " << endl;
getline(cin, *pUserInput);
continue;
}
}
bool SearchNames(string names[], int *pTotal, *pUserInput)
{
}
// FILE: P1_NameSearch_Header.h (Header file/prototype)
// 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 <iostream> // Needed for cin and cout
#include <string> // Needed for string values
using namespace std;
/* This is the function that will be responsible for displaying the program's
introduction, main objective, rules, and a brief user greeting. */
void Objective_screen(string *name);
/* This is the function that will return a Boolean value, which is checked
in main(). If the specified file can't be located, a message will be
displayed to inform user, and the program with exit. If the file is found,
the names[] array will be filled, and the total number of names that were
read are returned to the main() function. */
bool ReadFile(string names[], int *pTotal);
/* This function uses a modified version of the 'bubble sort' to sort
the names from the data file in a fashion that is requested. */
void bubbleSort(string names[], int *pTotal);
/* This function will use pointers and or references to obtain two names
from the functions and then pass the specified names. */
void AskForTwoNames(string *pUserInput);
/* The array, total number of names, and the user's specified selection
of two names is passed to this function (SearchNames()). Then it
will pass the results (whether the names were found and how many
times the names occurred). */
bool SearchNames(string names[], int *pTotal, *pUserInput);
// FILE: P1_NameSearch_Driver.cpp (Drive/main)
// 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 <iostream> // Needed for cin and cout
#include <string> // Needed for string values
#include <fstream> // Reading/Writing from file requires this standard library.
using namespace std;
#include "P1_NameSearch_Functions.cpp"
int main()
{
// Declared Variables
string names[800];
string pUserInput;
int pTotal;
// Function for the Objective Screen
Objective_screen(&name);
cout << "\n Data Contained In \"FirstNames.txt\" \n";
// Call ReadFile() function to read data (names) into names[];
// The ReadFile() function will return a false if the file can't be found.
bool ok = ReadFile(names, &pTotal);
// This loop is to make sure that the data file was opened successfully.
if(ok == false)
{
cout << "\n Opps! ERROR... Can't Continue Working, NO DATA! ";
cout << "\n =============================================== \n " << endl;
// Returned false, no file, and the program terminates.
exit(1);
}
/* This calls the bubbleSort() function from where the array names[]
is read and sorted via its values. */
void bubbleSort(names, &pTotal);
/* The function called AskForTwoNames() will use pointer and or
references to obtain two names from the functions. */
void AskForTwoNames(&pUserInput);
// After everything nessecary is complete, the file can be closed.
Input.close();
return 0;
}