Screw it... here is the full code:
Code:
// =========================================================== //
// PROGRAM: Assignment16.cpp
// AUTHOR:
// DATE:
// PURPOSE: This program sorts an array of words followed by
// the user being able to search for a word and read
// it meaning.
// ========================================================== //
#include <fstream.h>
#include "tstring.h"
const int MAXSIZE = 10;
// The following list the prototypes used by this application.
void getData(String A[], String B[], int& N);
void SortWords(String A[], String B[], int N);
void Swap(String& X, String& Y);
void DisplayWords(String A[], int N);
int SearchWord(String A[], String& Key, int N);
void main()
{
// The following objects are local to this function.
String Words[MAXSIZE]; // Array of words.
String Defines[MAXSIZE]; // Array of definitions
// for the words in array
// "Words."
String Key;
int Count, Index;
char Response;
// The following step reads unsorted words from a text file.
getData(Words, Defines, Count);
// The following sorts the words in ascending order using the
// selection sort.
SortWords(Words, Defines, Count);
// The following displays the words in the array after being
// sorted.
cout << "Dictionary: \n";
DisplayWords(Words, Count);
// The following allows the student to search for a word within
// the dictionary of words.
do
{
// Prompt for a word to search the array of words.
cout << "\nEnter word for searching: ";
cin >> Key;
Index = SearchWord(Words, Key, Count);
if( Index >= 0 )
{
// Report on the definition of the word.
cout << Words[Index] << ": "
<< Defines[Index] << endl;
}
else
// Report that the word does not exist.
cout << "This word is not in the array." << endl;
// Prompt the user to repeat another search.
cout << "\nRepeat this search process "
<< "(Enter 'Y' to continue): ";
cin >> Response;
cin.ignore(80, '\n');
} while( Response == 'Y' || Response == 'y' );
}
// --------------- Free Function Definitions ------------------
void getData(String A[], String B[], int& N)
// Purpose: This function enters the words into array A and the
// definitions into array B.
{
// The following are objects local to this function.
fstream infile;
String Word, Definition;
// Open the unsorted file of words. This file also contains
// their definitions.
infile.open("Data.dat", ios :: in);
if( !infile )
{
cout << "Input file does not exist.";
exit(0);
}
// Set the count on the number of words to zero.
N = 0;
// Continue to read the infile for the next word and its meaning.
while(true)
{
// Read the word and its definition.
infile >> Word;
infile.ignore(80, '\n');
getline(infile, Definition);
// Test if the end-of-file has been reached or the number
// of words read is equal to MAXSIZE.
if( infile.eof() || N == MAXSIZE ) break;
// Assign the last word read to array A and its definition
// to array B.
A[N] = Word;
B[N] = Definition;
// Increment the word counter by one.
++N;
}
return;
}
void SortWords(String A[], String B[], int N)
// Purpose: This function sorts array A in ascending order using
// the selection sort algorithm.
{
for(int Pass = 0; Pass < N - 1; ++Pass)
{
// Establish the initial values for temp and mark.
String temp = A[Pass];
int mark = Pass;
// Compare the remaining unsorted elements of array A
// with temp.
for( int I = Pass + 1;I < N; ++I)
{
if( temp > A[I])
{
mark = I;
temp = A[I];
}
}
// Swap the smallest element of the remaining part of the
// array with A[pass].
Swap(A[Pass], A[mark]);
Swap(B[Pass], B[mark]);
}
}
void Swap(String& X, String& Y)
// Purpose: This function interchange the values represented
// by X and Y.
{
String temp = X;
X = Y;
Y = temp;
}
void DisplayWords(String A[], int N)
// Purpose: Display the list of words stored in array A.
{
for(int I = 0; I < N; ++I)
{
cout << A[I] << endl;
}
}
int SearchWord(String A[], String& Key, int N)
// Purpose: This function will search for the position of the
// Key stored in array A. It uses the steps of the
// BinarySearch algorithm.
{
// Initialize the indices Low and High.
int Middle;
int Low = 0;
int High = N -1;
// While Low is not greater than High, search for a match.
while ( Low <= High )
{
// Determine the middle of the table.
Middle = (Low + High) / 2;
// Check if the middle array element has a key that
// matches Key.
if( Key == A[Middle])
return Middle;
else
// Determine which half of the array to search.
if( Key < A[Middle])
High = Middle - 1;
else
Low = Middle + 1;
}
return ( -1 );
}