Hello! My name is Page, and I am having problems figuring out how to read a name for example like; SMITH, JOHN in a string array. From there I would like to find the space and read the last name into a vector and the first name into another vector. From there I would like to search for the users specified input and return how many I have found (It can also include a name such as JOHNSTON, if the user was searching for JOHN, or JOHN JOHNSTON would return 2 JOHN's) Thank you I appreciate it. I am having a hard time with the strings.
Here's my code:
Header File
// CLASS: CIS 2275 C++ II
// PROGRAM: Program #2-A (Revision) | C++ 10K NAME SEARCH
// E-MAIL: ppotter03@inbox.com
// FILE: P2-A_Functions.h
#include <string>
#include <vector>
using namespace std;
#define FILE_IN "Names10K.txt"
/* This function will be responsible for displaying the programs name,
objective, and introduction. */
void Objective_screen();
/* This function will search for the file that contains the specified
data from which the program depends upon. If the file is located
the information is stored in the specifed array and program continues.
If not, the program will exit. */
bool ReadFile(string Names[], int &rTotal);
/* This function uses a modified version of the 'bubble sort' to sort
the names from the data file. */
// void bubbleSort();
void Sort(string Names[], int rTotal);
/* This function will use references to obtain the user's input from the
function and then store the user's input for use later. */
void AskForName(string &rUserInput);
/* This function will use the stored user input for a search objective.
The search objective will be compared with the contents of the data
file for matching occurences. */
void SearchNames(string Names[], int rTotal, string UserInput, vector<string> &rvFirst,
vector<string> &rvLast);
/* This function takes the values that were obtained from the first few
parts of the program, and write them to a summary file. After the data
is written to the specified file the program will output the results to
the screen. */
bool WriteOutput(string Names[], int rTotal);
Functions
// CLASS: CIS 2275 C++ II
// PROGRAM: Program #2-A (Revision) | C++ 10K NAME SEARCH
// E-MAIL: ppotter03@inbox.com
// FILE: P2-A_Functions.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// Specified data files
#define FILE_IN "Names10K.txt"
#define FILE_OUT "Names10K_Results.txt"
void Objective_screen()
{
// Declared variables
char ENTER;
// Author, program title, and program objective.
cout << "\n -------------------------------------------------------------- ";
cout << "\n C++ 10K NAME SEARCH ";
cout << "\n -------------------------------------------------------------- ";
cout << "\n AUTHOR: Page Lynn Potter ";
cout << "\n CLASS: CIS 2275 ";
cout << "\n ASSIGNMENT: Program #2-A ";
cout << "\n -------------------------------------------------------------- \n";
cout << "\n This program will read 10,000 names (first and last) from a "
<< "\n file, and store in an array. The names are counted and sorted "
<< "\n (last, first). The user than enters in a name to search for, "
<< "\n and the program searchs the names in the file. The search "
<< "\n results are then reported to the user and also written to a "
<< "\n program summary file. \n";
cout << "\n TO BEGIN, PLEASE HIT THE ENTER KEY... \n";
// Wait for user's input
cin.get(ENTER);
}
bool ReadFile(string Names[], int &rTotal)
{
ifstream input;
// c_str() to convert to char[] format
input.open(FILE_IN);
if (! input)
{
return false;
}
rTotal = 0;
// Counts the names in the file until it reaches the end.
while (! input.eof())
{
FileInput >> Names[rTotal];
++rTotal;
}
FileInput.close();
return true;
}
void Sort(string Names[], int rTotal)
{
// Test the unsorted array
for (int i = 0; i < 10; ++i)
{
cout << "\n Unsorted First Ten Names: " << Names[i];
}
string temporary;
for(int A = 0; A < (rTotal - A); ++A)
{
for(int B = 1; B < rTotal; ++B)
{
if(Names[B - 1] > Names[B])
{
temporary = Names[B];
Names[B] = Names[B - 1];
Names[B - 1] = temporary;
}
}
}
// Test the sorted array
for (int i = 0; i < 10; ++i)
{
cout << "\n Sorted First Ten Names: " << Names[i];
}
}
void AskForName(string &rUserInput)
{
cout << "\n In CAPS, Enter In Name You'de Like To Search For: ";
getline(cin, rUserInput);
cin.ignore();
// int index = rUserInput.find(" ");
//rLast = rUserInput.substr(0,index-1);
//rFirst = rUserInput.substr(index+1);
}
void SearchNames(string Names[], int rTotal, string rUserInput, vector<string>
&rvFirst, vector<string> &rvLast)
{
// Need to fix code below....
for (int i = 0; i < rTotal; ++i)
{
int pos;
pos = Names[i].find(rUserInput);
int sppos;
sppos = Names[i].find(" ");
if( pos == -1)
{
continue;
}
else
{
break;
}
if( pos < sppos)
{
rvFirst.push_back(Names[i]);
}
if( pos < sppos)
{
rvFirst.push_back(Names[i]);
}
}
}
bool WriteOutput(string Names[], int rTotal)
{
string OutputFile = "Names10K_Results.txt";
ofstream FileOutput;
FileOutput.open(OutputFile.c_str());
// Tests for output file
if(!FileOutput)
{
cout << "\n Opps! Error... Can't Locate The File \"" << OutputFile << "\"";
cout << "\n ----------------------------------------------------- \n " << endl;
return false;
}
else
{
cout << "\n Found Data Output File! \n";
return true;
}
FileOutput << "\n -------------------------------------------------------------- ";
FileOutput << "\n C++ 10K NAME SEARCH ";
FileOutput << "\n -------------------------------------------------------------- ";
FileOutput << "\n AUTHOR: Page Lynn Potter ";
FileOutput << "\n CLASS: CIS 2275 ";
FileOutput << "\n ASSIGNMENT: Program #2-A ";
FileOutput << "\n -------------------------------------------------------------- \n";
FileOutput << "\n -------------------------------------------------------------- \n";
FileOutput << "\n RESULTS: ";
cout << "\n Check Output! ";
FileOutput.close();
}
// END OF P2-A_Functions
Driver
// CLASS: CIS 2275 C++ II
// PROGRAM: Program #2-A (Revision) | C++ 10K NAME SEARCH
// E-MAIL: ppotter03@inbox.com
// FILE: P2-A_Driver.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
#include "P2-A_Functions.h"
int main()
{
// Declared Variables
string Names[10000], answer, rUserInput;
string IN_FILE = "Names10K.txt", OUT_FILE = "Names10K_Results.txt";
int rTotal = 0;
vector<string> rvFirst;
vector<string> rvLast;
Objective_screen();
do{
bool OK = ReadFile(Names, rTotal);
if(!OK)
{
cout << "\n ERROR! Can't Find The Input File: \"" << IN_FILE << "\"";
cout << "\n .......Program Will Now Terminate....... \n";
// If there is an error, this terminates program.
exit(1);
}
else
{
cout << "\n SUCCESS! Found The Input File: \"" << IN_FILE << "\"";
cout << "\n There's a Total Of " << rTotal << " Names In The File. \n" << endl;
}
Sort(Names, rTotal);
AskForName(rUserInput);
SearchNames(Names, rTotal, rUserInput, rvFirst, rvLast);
SearchNames(Names, rTotal, rUserInput, rvFirst, rvLast);
//if(result == true)
//{
//cout << "\n ERROR! Can't Find The Output File: \"" << OutputFile << "\"";
//}
//else
//{
//cout << "\n Opps! Error... Problem Writing To File. ";
//exit(1);
//}
cout << "\n Search For More Name(s)? (YES / NO) ";
getline(cin, answer);
}while(answer == "YES");
return(0);
}
// END OF C++ 10K NAME SEARCH