Hello! How is everyone? I have been working on this for about a week and a half, and I need it to function properly. I am going to be using this program to build my next assignment, thus needing it to work properly.

My problem: I am trying to write a program that uses a .txt file with about roughly 732 first names. I would like to ask the user input two first names of their choice, and the program is required to search for them (No fancy algorithms required, just a basic straight down the list "brute force" search.).
I get the program to run though just fine except my results of the search return that neither of the names are found. Which I know these names are in the list, and I am greatly troubled by this program. If there are any issues right off the bat with my code let me know. If my logic of the code is a bit choppy tell me how I can go about and fix it.
The one problem keeping my program from properly working is the search function, everything else is functional. Any help to fix my search function will be greatly appreciated, Thank You!

MY HEADER FILE

// FILE: P1_NameSearch_Functions.h
// This program will cover strings, arrays, file I/O, pointers/references.
// OBJECTIVE: Read first names data & search for the corresponding user input. 

#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 *pName);

/* 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 &rTotal);

/* 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 &rTotal);

/* This function will use pointers and or references to obtain two names
   from the functions and then pass the specified names. */
void AskForTwoNames(string &rNameONE, string &rNameTWO);

/* 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). */
void SearchNames(string names[], int &rTotal, string &rNameONE, string &rNameTWO,
				 int &rCount_NameONE, int &rCount_NameTWO, bool result_NameONE,
				 bool result_NameTWO);

/* The WriteOutput() function takes the values (User Input) that was 
   passed to the function, and displays the results as well as outputs
   the results to the specified output file. The output file will
   serve as a brief summary for the user; if they desire to open it
   and read the information. */
bool WriteOutput(string *pName, string names[], int &rTotal, string &rNameONE, 
				 string &rNameTWO, int &rCount_NameONE, int &rCount_NameTWO,
				 bool result_NameONE, bool result_NameTWO);

MY FUNCTION PROTOTYPE

// FILE: P1_NameSearch_Functions.cpp
// This program will cover strings, arrays, file I/O, pointers/references.
// OBJECTIVE: Read first names data & search for the corresponding user input. 

#include <iostream>				// Needed for cin and cout
#include <string>				// Needed for string values
#include <fstream>              // Reading/Writing file requires this standard library.
#include <string.h>

using namespace std;

// For easier access #define is used for file names
#define FILE_IN "FIRST-NAMES.txt"
#define FILE_OUT "FIRST-NAMES_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     AUTHOR:		Page Lynn Potter ";
	cout << "\n     E-MAIL:		ppotter3@cnm.edu   or	ppotter03@inbox.com ";
	cout << "\n      CLASS:		CIS 2275 ";
	cout << "\n ASSIGNMENT:		Program #1 \n";
	cout << "\n -------------------------------------------------------------- ";
	cout << "\n       C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH ";
	cout << "\n -------------------------------------------------------------- ";

	cout << "\n                  HELLO!                      ";
	cout << "\n My name is Page, and I will go over the main "
		 << "\n objective and instructions for my 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 will make use of input/output files for the "
		 << "\n census data. This program will search the census data for "
		 << "\n user specified occurences of popular first names. ";
	cout << "\n This program will visually narrate the steps that this "
		 << "\n program goes through to achieve the final results. \n";

	cout << "\n TO START THE PROGRAM HIT THE ENTER TO BEGIN... \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 names[] array,
	   after the data file has been successfully located.*/

	// Declared Variables
	string filename = "FIRST-NAMES.txt";
	
	/* Uses the 'open' member function of the ifstream object to
	   open a file for reading/input. */
	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... Cannot Locate \"" << FILE_IN << "\"";
		cout << "\n ----------------------------------------------------- \n " << endl;
		exit(1);
		// Returns the false value (or failure) if file can't be found.
		return false;
	}

	/* First names from the text file need to read into the specified
	   array and couted until the program reaches the end of the
	   specified file. */
	rTotal = 0;

	/* Needed to make sure the names are counted in the .txt file.
	   The &rTotal 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;

	// All done reading, close the file
	FirstNamesInput.close();

	// 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) loops
	for(int i = 0; i < rTotal - 1; ++i)							
	{
		// 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]
				temp = names[j];							
				// Array element is assigned the value of [j - 1]
				names[j] = names[j-1];						
				// Element [j - 1] is replaced by temp
				names[j-1] = temp;								
			}
		}
	}
}
 
void AskForTwoNames(string &rNameONE, string &rNameTWO)
{
	// This function will ask the user to input two first names of thier choice.
	cout << "\n Enter in two first names, in CAPS, seperated by a space:      ";
	cin  >> rNameONE >> rNameTWO;
	
	// This was to be used if I had to collect both names into one variable.
    // int index = rUserInput.find(" ");
    // rNameONE = rUserInput.substr(0,index-1);
    // rNameTWO = rUserInput.substr(index+1);
}

void SearchNames(string names[], int &rTotal, string &rNameONE, string &rNameTWO, 
				 int &rCount_NameONE, int &rCount_NameTWO, bool result_NameONE,
				 bool result_NameTWO)
{
	/* This is the search function that implements a basic brut force
	   search to find the specified names that the user inputs. */
	rCount_NameONE = 0;
	rCount_NameTWO = 0;

	result_NameONE = false;
	result_NameTWO = false;

		for(int i = 0; i < rTotal; ++i)
		{
		   //assume names[] is the array that holds the sorted names

		   if(rNameONE == names[i])
		   {
				 // Increment the counter to record occurences of name
				 ++rCount_NameONE;
				 result_NameONE = true;
				 continue;
			}
		   else if(names[i] > rNameONE)
		   {
			   break;
		   }
			else
			{	
				result_NameONE = false;
				//break;
			 }

		}


		   for(int j = 0; j < rTotal; ++j)
		   {
			   if(rNameTWO == names[j])
			   {
				   // Increment the counter to record occurences of name
				   ++rCount_NameTWO;
				   result_NameTWO = true;
				   continue;
			   }
			    else if(names[j] > rNameTWO)
				{
					break;
				}
				else
				{	
					result_NameTWO = false;
					//break;
				}

			}
		
}										

bool WriteOutput(string *pName, string names[], int &rIndex, string &rNameONE, 
				 string &rNameTWO, int &rCount_NameONE, int &rCount_NameTWO,
				 bool result_NameONE, bool result_NameTWO)
{
	/* 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. */
	
	// Declared Variables
	string filename = "FIRST-NAMES_SUMMARY.txt";
	result_NameONE = false;
	result_NameTWO = false;
	
	// 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... Cannot Locate \"" << FILE_OUT << "\"";
		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! ";
		cout << "\n THE C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH "
			 << "\n Has Located The Data Output File! ";
	
		FirstNamesOutput << "\n         AUTHOR:		Page Lynn Potter ";
		FirstNamesOutput << "\n          CLASS:		CIS 2275 ";
		FirstNamesOutput << "\n     ASSIGNMENT:		Program #1 \n";
		FirstNamesOutput << "\n -------------------------------------------------------------- ";
		FirstNamesOutput << "\n       C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH ";
		FirstNamesOutput << "\n -------------------------------------------------------------- \n";
		FirstNamesOutput << "\n ------------RESULTS------------ \n";
		FirstNamesOutput << "\n    USER'S NAME:	    " <<   *pName;	
		FirstNamesOutput << "\n   USER'S INPUT:		" << rNameONE << "    " << rNameTWO;  
	}
	if(result_NameONE == true)
	{
					cout << "\n" << rNameONE << " Is In The List," << rCount_NameONE << "Times. ";	
		FirstNamesOutput << "\n" << rNameONE << " Is In The List," << rCount_NameONE << "Times. ";
	}
	else if(result_NameTWO == true)
	{
					cout << "\n" << rNameTWO << " Is In The List," << rCount_NameTWO << "Times. ";
		FirstNamesOutput << "\n" << rNameTWO << " Is In The List," << rCount_NameTWO << "Times. ";
	}
	else
	{
					cout << "\n" << rNameONE << " Is Not In The List. ";
					cout << "\n" << rNameTWO << " Is Not In The List. ";
		FirstNamesOutput << "\n" << rNameONE << " Is Not In The List. ";
		FirstNamesOutput << "\n" << rNameTWO << " Is Not In The List. ";
	}

	// Reading and writing complete, closing file.
	FirstNamesOutput.close();
	
	return true;
}
// END OF C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH

MY PROGRAM DRIVER

// FILE: P1_NameSearch_Driver.cpp
// This program will cover strings, arrays, file I/O, pointers/references.
// OBJECTIVE: Read first names data & search for the corresponding user input. 

#include <iostream>				// Needed for cin and cout
#include <string>				// Needed for string values
#include <fstream>              // Reading/Writing file requires this standard library.

using namespace std;

#include "P1_NameSearch_Functions.h"

int main()
{
	// Declared Variables
		string names[800], pName, rNameONE, rNameTWO, answer;
		int rTotal, rCount_NameONE, rCount_NameTWO;
		bool result_NameONE = false;
		bool result_NameTWO = false;
	
		// Function for the Objective Screen
		Objective_screen(&pName);

	do {
		
		// Call ReadFile() function to read data (names) into names[];
		// The ReadFile() function will return a false if the file can't be found.
		bool result = ReadFile(names, rTotal);

		if(result == false)
		{
			cout << "\n Opps! Error... Can't Continue Working, No Data!";
			// No input file to read, program must terminate.
			exit(1);
		}
			
		/* This calls the bubbleSort() function from where the array names[]
		   is read and sorted via its values. */
		bubbleSort(names, rTotal);

		/* The function called AskForTwoNames() will use pointer and or 
		   references to obtain two names from the functions. */
		AskForTwoNames(rNameONE, rNameTWO);

		SearchNames(names, rTotal, rNameONE, rNameTWO, rCount_NameONE, 
				    rCount_NameTWO, result_NameONE, result_NameTWO);
		
		result = WriteOutput(&pName, names, rTotal, rNameONE, rNameTWO,
				 rCount_NameONE, rCount_NameTWO, result_NameONE, result_NameTWO);
		if(result == false)
		{
			cout << "\n Opps! Error... Problem Writing Results. ";
		}
		else 
		{
			cout << "\n Finished Writing Data Results! ";
			cout << "\n Check \"FIRST-NAMES_SUMMARY.txt\" For Program Results! ";
		}
		
		// Ask the user if they would like to search names again.
		cout << "\n Would You Like To Search Again? (YES / NO) ";
		getline(cin, answer);
		cin.ignore();
		
		}while (answer == "YES");

		return 0;
}

Recommended Answers

All 3 Replies

void SearchNames(string names[], int &rTotal, string &rNameONE, string &rNameTWO, 
				 int &rCount_NameONE, int &rCount_NameTWO, bool result_NameONE,
				 bool result_NameTWO)
{
	/* This is the search function that implements a basic brut force
	   search to find the specified names that the user inputs. */
	rCount_NameONE = 0;
	rCount_NameTWO = 0;

	result_NameONE = false;
	result_NameTWO = false;

		for(int i = 0; i < rTotal; ++i)
		{
		   //assume names[] is the array that holds the sorted names

		   if(rNameONE == names[i])
		   {
				 // Increment the counter to record occurences of name
				 ++rCount_NameONE;
				 result_NameONE = true;
				 continue;
			}
		   else if(names[i] > rNameONE)
		   {
			   break;
		   }
			else
			{	
				result_NameONE = false;
				//break;
			 }

		}


		   for(int j = 0; j < rTotal; ++j)
		   {
			   if(rNameTWO == names[j])
			   {
				   // Increment the counter to record occurences of name
				   ++rCount_NameTWO;
				   result_NameTWO = true;
				   continue;
			   }
			    else if(names[j] > rNameTWO)
				{
					break;
				}
				else
				{	
					result_NameTWO = false;
					//break;
				}

			}
		
}										

bool WriteOutput(string *pName, string names[], int &rIndex, string &rNameONE, 
				 string &rNameTWO, int &rCount_NameONE, int &rCount_NameTWO,
				 bool result_NameONE, bool result_NameTWO)
{
	/* 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. */
	
	// Declared Variables
	string filename = "FIRST-NAMES_SUMMARY.txt";
	result_NameONE = false;
	result_NameTWO = false;
	
	// 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... Cannot Locate \"" << FILE_OUT << "\"";
		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! ";
		cout << "\n THE C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH "
			 << "\n Has Located The Data Output File! ";
	
		FirstNamesOutput << "\n         AUTHOR:		Page Lynn Potter ";
		FirstNamesOutput << "\n          CLASS:		CIS 2275 ";
		FirstNamesOutput << "\n     ASSIGNMENT:		Program #1 \n";
		FirstNamesOutput << "\n -------------------------------------------------------------- ";
		FirstNamesOutput << "\n       C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH ";
		FirstNamesOutput << "\n -------------------------------------------------------------- \n";
		FirstNamesOutput << "\n ------------RESULTS------------ \n";
		FirstNamesOutput << "\n    USER'S NAME:	    " <<   *pName;	
		FirstNamesOutput << "\n   USER'S INPUT:		" << rNameONE << "    " << rNameTWO;  
	}
	if(result_NameONE == true)
	{
					cout << "\n" << rNameONE << " Is In The List," << rCount_NameONE << "Times. ";	
		FirstNamesOutput << "\n" << rNameONE << " Is In The List," << rCount_NameONE << "Times. ";
	}
	else if(result_NameTWO == true)
	{
					cout << "\n" << rNameTWO << " Is In The List," << rCount_NameTWO << "Times. ";
		FirstNamesOutput << "\n" << rNameTWO << " Is In The List," << rCount_NameTWO << "Times. ";
	}
	else
	{
					cout << "\n" << rNameONE << " Is Not In The List. ";
					cout << "\n" << rNameTWO << " Is Not In The List. ";
		FirstNamesOutput << "\n" << rNameONE << " Is Not In The List. ";
		FirstNamesOutput << "\n" << rNameTWO << " Is Not In The List. ";
	}

	// Reading and writing complete, closing file.
	FirstNamesOutput.close();
	
	return true;
}
// END OF C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH

First of all I think over the top commenting can sometimes make the code quite unreadable, I prefer using block comment above a function describing what it does instead of comments scattered all over the place. But that's your choice.

You could have easily debugged this problem had you put some couts at a few places. The problem is with the bool values, result_NameONE and result_NameTWO, in these 2 functions above. Can you see it now??

Why do you continue once you have a match? Just break right there.

Two main problems:
1) WriteOutput() is destroying the value of result_NameONE and result_NameTWO. Delete those two lines at the beginning of that function because they are set in the Search function.

2) SearchNames() -- the last two parameters must be passed by reference. That function is also much more complicated than it needs to be. Here's another way to do it

void FindName(string names[], int rTotal, string sname, int &count, bool& result)
{
    for(int i = 0; i < rTotal; i++)
    {
        if( names[i] == sname)
        {
            count = i;
            result = true;
            break;
        }
    }
}
void SearchNames(string names[], int &rTotal, string &rNameONE, string &rNameTWO, 
				 int &rCount_NameONE, int &rCount_NameTWO, bool& result_NameONE,
				 bool& result_NameTWO)
{
	/* This is the search function that implements a basic brut force
	   search to find the specified names that the user inputs. */
	rCount_NameONE = 0;
	rCount_NameTWO = 0;

	result_NameONE = false;
	result_NameTWO = false;
    FindName(names, rTotal, rNameONE, rCount_NameONE, result_NameONE);
    FindName(names, rTotal, rNameTWO, rCount_NameTWO, result_NameTWO);

		
}

Hey guys! I GOT IT WORKING!!!!!!!!!! Thank you! I had to change a bunch of stuff. Here it is:

// FILE: P1_NameSearch_Functions.h
// This program will cover strings, arrays, file I/O, pointers/references.
// OBJECTIVE: Read first names data & search for the corresponding user input. 

#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 *pName);

/* 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 &rTotal);

/* 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 &rTotal);

/* This function will use pointers and or references to obtain two names
   from the functions and then pass the specified names. */
void AskForTwoNames(string &rNameONE, string &rNameTWO);

/* 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). */
void SearchNames(string names[], int rTotal, string rNameONE, string rNameTWO, 
				 int &rCount_NameONE, int &rCount_NameTWO);

/* The WriteOutput() function takes the values (User Input) that was 
   passed to the function, and displays the results as well as outputs
   the results to the specified output file. The output file will
   serve as a brief summary for the user; if they desire to open it
   and read the information. */
bool WriteOutput(string *pName, string names[], int rTotal, string rNameONE, 
				 string rNameTWO, int &rCount_NameONE, int &rCount_NameTWO);
// FILE: P1_NameSearch_Functions.cpp
// This program will cover strings, arrays, file I/O, pointers/references.
// OBJECTIVE: Read first names data & search for the corresponding user input. 

#include <iostream>				
#include <string>				
#include <fstream>              
#include <string.h>

using namespace std;

// #define is used to access file names
#define FILE_IN  "FIRST-NAMES.txt"
#define FILE_OUT "FIRST-NAMES_SUMMARY.txt"

void Objective_screen(string *pName)
	{
	// Declared variables
	char ENTER;

	// Greeting and introduction to the program
	cout << "\n     AUTHOR:		Page Lynn Potter ";
	cout << "\n     E-MAIL:		ppotter3@cnm.edu   or	ppotter03@inbox.com ";
	cout << "\n      CLASS:		CIS 2275 C++ II ";
	cout << "\n ASSIGNMENT:		Program #1 \n";
	cout << "\n -------------------------------------------------------------- ";
	cout << "\n       C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH ";
	cout << "\n -------------------------------------------------------------- \n";

	cout << "\n                  HELLO!                      ";
	cout << "\n My name is Page, and I will go over the main "
		 << "\n objective and instructions for my 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 will make use of input/output files for the "
		 << "\n first name data that will be used in this program. ";
	cout << "\n The program will search a file that contains first "
		 << "\n names; and the data was taken from the previous year's "
		 << "\n census records. \n";
	cout << "\n The user will be asked to enter in their choice of common "
		 << "\n first names. Next, the user's specified first names will "
		 << "\n be compared to the census data for comparison. ";
	cout << "\n The results will be recored and displayed to the user; "
		 << "\n and if the names were found the user will see how many "
		 << "\n times the names occured. \n";

	cout << "\n TO START THE PROGRAM HIT THE ENTER TO BEGIN... \n";
	// Wait for user's input
	cin.get(ENTER);
}

bool ReadFile(string names[], int &rTotal)
{
	// Declared Variables
	string filename = "FIRST-NAMES.txt";
	
	ifstream FirstNamesInput;

	// c_str() to convert to char[] format
	FirstNamesInput.open(filename.c_str()); 

	if(!FirstNamesInput)
	{
		cout << "\n		Opps! Error... Cannot Locate \"" << FILE_IN << "\"";
		cout << "\n ----------------------------------------------------- \n " << endl;
		exit(1);

		return false;
	}

	rTotal = 0;

	// Counts the names in the specified list.
	while(FirstNamesInput >> names[rTotal])
		++rTotal;

	FirstNamesInput.close();

	return true;
}

void bubbleSort(string names[], int &rTotal)
{

	// Declared Variables
	string temp;
	
	// First of dual (nested) loops
	for(int i = 0; i < rTotal - 1; ++i)							
	{
		// Second of dual (nested) loop
		for(int j = 1; j < rTotal; ++j)							
		{
			// To determine if [j - 1] is larger than [j]. 
			if(names[j-1] > names[j])
			{
				
				temp = names[j];							
				
				names[j] = names[j-1];						
				
				names[j-1] = temp;								
			}
		}
	}
}
 
void AskForTwoNames(string &rNameONE, string &rNameTWO)
{
	cout << "\n Please, enter in two first names, in CAPS, seperated by a space:  ";
	cin  >> rNameONE >> rNameTWO;
	
	// This was to be used if I had to collect both names into one variable.
    // int index = rUserInput.find(" ");
    // rNameONE = rUserInput.substr(0,index-1);
    // rNameTWO = rUserInput.substr(index+1);
}

void SearchNames(string names[], int rTotal, string rNameONE, string rNameTWO, 
				 int &rCount_NameONE, int &rCount_NameTWO)
{
	// Declared Variables
	rCount_NameONE = 0;
	rCount_NameTWO = 0;

	for (int i = 0; i < rTotal; ++i)
	{
		if (rNameONE == names[i])
		{
			++rCount_NameONE;
		}
		else if (rCount_NameONE > 0)
		{
			break;
		}

		if (rNameTWO == names[i])
		{
			++rCount_NameTWO;
		}
		else if (rCount_NameTWO > 0)
		{
			break;
		}
	}
}
			
bool WriteOutput(string *pName, string names[], int rTotal, string rNameONE, 
				 string rNameTWO, int &rCount_NameONE, int &rCount_NameTWO)
{
	// Declared Variables
	string filename = "FIRST-NAMES_SUMMARY.txt";
	
	ofstream FirstNamesOutput;	

	FirstNamesOutput.open(filename.c_str());								

	// Tests for output file
	if(!FirstNamesOutput)
	{
		cout << "\n		Opps! Error... Cannot Locate \"" << FILE_OUT << "\"";
		cout << "\n ----------------------------------------------------- \n " << endl;
		exit(1);
		
		return false;
	}
	else
	{
		cout << "\n THE C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH, "
			 << "\n Has Located The Data Output File! \n";
	
		FirstNamesOutput << "\n         AUTHOR:		Page Lynn Potter ";
		FirstNamesOutput << "\n          CLASS:		CIS 2275 ";
		FirstNamesOutput << "\n     ASSIGNMENT:		Program #1 \n\n";
		FirstNamesOutput << "\n -------------------------------------------------------------- ";
		FirstNamesOutput << "\n       C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH ";
		FirstNamesOutput << "\n -------------------------------------------------------------- \n\n";
		FirstNamesOutput << "\n ------------RESULTS------------ \n\n";
		FirstNamesOutput << "\n    USER'S NAME:	    " <<   *pName;	
		FirstNamesOutput << "\n   USER'S INPUT:		" << rNameONE << "    " << rNameTWO;  
	}

	// If counter returns 0, name is not found
	if (rCount_NameONE == 0)
	{
					cout << "\n SEARCH RESULTS:    " << rNameONE << " Is Not In The List. \n";
		FirstNamesOutput << "\n SEARCH RESULTS:    " << rNameONE << " Is Not In The List. \n";
	}
	else if (rCount_NameONE >= 1)
	{
					cout << "\n SEARCH RESULTS:    " << rNameONE << " Is In The List, " << rCount_NameONE << " Times. \n";	
		FirstNamesOutput << "\n SEARCH RESULTS:    " << rNameONE << " Is In The List, " << rCount_NameONE << " Times. \n";
	}
	if (rCount_NameTWO == 0)
	{
					cout << "\n SEARCH RESULTS:    " << rNameTWO << " Is Not In The List. \n";
		FirstNamesOutput << "\n SEARCH RESULTS:    " << rNameTWO << " Is Not In The List. \n";	
	}
	else if (rCount_NameTWO >= 1)
	{
					cout << "\n SEARCH RESULTS:    " << rNameTWO << " Is In The List, " << rCount_NameTWO << " Times. \n";
		FirstNamesOutput << "\n SEARCH RESULTS:    " << rNameTWO << " Is In The List, " << rCount_NameTWO << " Times. \n";	
	}
	
	FirstNamesOutput.close();
	
	return true;
}
// END OF C++ U.S. CENSUS DATA MOST COMMON FIRST NAME SEARCH
// FILE: P1_NameSearch_Driver.cpp
// This program will cover strings, arrays, file I/O, pointers/references.
// OBJECTIVE: Read first names data & search for the corresponding user input. 

#include <iostream>				
#include <string>				
#include <fstream>              

using namespace std;

#include "P1_NameSearch_Functions.h"

int main()
{
		// Declared Variables
		string names[800], pName, rNameONE, rNameTWO, answer;
		int rTotal, rCount_NameONE, rCount_NameTWO;
		
		Objective_screen(&pName);

	do {
		
		bool result = ReadFile(names, rTotal);
		cout << "\n There are a total of " << rTotal << " names in the file. " << endl;

		if(result == false)
		{
			cout << "\n Opps! Error... Can't Continue Working, No Data!";
			// No input file to read, program must terminate.
			exit(1);
		}
			
		bubbleSort(names, rTotal);

		AskForTwoNames(rNameONE, rNameTWO);

		SearchNames(names, rTotal, rNameONE, rNameTWO, rCount_NameONE, 
			        rCount_NameTWO);
		
		result = WriteOutput(&pName, names, rTotal, rNameONE, rNameTWO, 
			                 rCount_NameONE, rCount_NameTWO);

		if(result == false)
		{
			cout << "\n Opps! Error... Problem Writing Results. ";
		}
		else 
		{
			cout << "\n Finished Writing Data Results! ";
			cout << "\n Check \"FIRST-NAMES_SUMMARY.txt\" For Program Results! ";
		}

		cout << "\n Would You Like To Search Again? (YES / NO) ";
		getline(cin, answer);
		cin.ignore();
		
		}while (answer == "YES");

		return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.