Hello! I have a question, I have been struggling with this for a bit (I welcome any constructive criticism and pointers) now, and I am pretty new to C++. I am in the process of writing a program that takes names in the format of LAST, FIRST; the it loads the names into a string array (Names[10000]). I first fill the array after reading the file, this is done in ReadFile(), the I sort the filled array in Sort(), from sort I send the sorted names (sorted by LAST names) to the AskForName(). Here I ask the user to input the desired name they would like to search for. From the AskForName(), I send the input to SearchNames(). In this function I want to determine if the name is LAST or FIRST. If LAST I load the answer into a vector. If it is FIRST I load into another vector. This is where my problem is (I think anyway), I have it started but I am not sure if its correct. Now If I am on the right track how do I get the results to the WriteOutput() function? Thank you once again and here is the files.


HEADER FILE

// 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);

FUNCTION

// 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())
	{
		// Read names from file using ',' for delimiter?
		getline(input, Names[rTotal]);
		
		input >> Names[rTotal];
		

		++rTotal;
	}

	input.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....

// RIGHT, DO A FOR LOOP AND GO THROUGH THE WHOLE ARRAY ONCE.
// THE TRICK WILL BE TO LOAD THE FIRST AND THE LAST NAMES AT THE SAME TIME.
// SINCE THEY ARE ALREADY SORTED (IN NAMES ARRAY) IF YOU LOAD AS YOU GO, THEY'LL
// ALREADY BE SORTED.  MAKE SENSE TO YOU???



	for (int i = 0; i < rTotal; ++i)
		{
			int pos;
			pos = Names[i].find(rUserInput);

			int sppos;
			sppos = Names[i].find(" ");

			//int pos2;
			//pos2 = Names[i].find(sppos, rUserInput);

			
			// If pos == -1 than continue, name isn't in the list.
			if( pos == -1)
				{
					continue; 
				}
			// If pos != -1 than the name is found.
			else  
				{
					
				}
			// If pos < sppos, then it's a last, push into vector.
			if( pos < sppos)
				{
					rvLast.push_back(Names[i]);	
				}
			
			// If pos > sppos, then it's a first and push into vector. 
			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

// 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

Let me know if there are any issues or any improvements I can make in this program so It will function properly. Thank you.

Recommended Answers

All 2 Replies

Haven't looked at it in details, but I see that your search function is a linear
search. Make it a binary search since the array should be sorted already. You will see that
using the linear search you will have to do about on average N/2, where N is the total names,
while in binary search you do about log2(N). In low numbers for N it does not make big
difference, but if N was say 1 million, by using linear search you will have to make in average
500,000 comparisons, while in binary search you will make log2(1000000) which equals about 19 comparisons.

I have pondered the binary search several times, but I was told to use linear for the time being....

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.