A Solution to Lab #6

/////////////////////////////////////////////////////////////////
// Student Name: Mick Shrimpton
// Student ID  : 123-45-6789
// Course      : CS 161
// Assignment  : #6
// Program File: program6.cpp
// Due Date    : 8-14-97
// 
//  Description: This program acts as a spell checker. It reads one file
//               of correctly spelled words and one file of words whose 
//               spelling we need to check. We compare the two to find 
//               misspelled words, then output the misspelled words to a 
//               third file.
/////////////////////////////////////////////////////////////////

#include <iostream.h>   // for cout/cin
#include <iomanip.h>    // for manipulators
#include <string.h>     // for strcpy(), strlen(), strcmp()
#include <ctype.h>      // for toupper()
#include <fstream.h>    // for ifstream, ofstream
#include <assert.h>     // for assert()

const int MAX_FILENAME = 50;  // maximum length of filename
const int MAX_WORD = 20;      // maximum length of a word

////////////////////////////////////////////////////////////////////////
// Function: getFilenames
//           Prompts the user for 3 filenames; a dictionary, an
//           input file (containing words to be checked) and
//           an output file to write misspelled words to.
//
//  Inputs: none
//
// Outputs: a string representing the filename of the dictionary
//          a string representing the filename of the input file
//          a string representing the filename of the output file
//
void getFilenames(char dictionary[], char inputFile[], char outputFile[]);


////////////////////////////////////////////////////////////////////////
// Function: spellCheckWords
//           Checks the words in the input file for correct spelling.
//           Calls lookupWord for each input word. Writes misspelled
//           words to the output file.
//
//  Inputs: a string representing the filename of the dictionary
//          a string representing the filename of the input file
//          a string representing the filename of the output file
//
// Outputs: none
//
void spellCheckWords(const char dictionary[], 
                     const char inputFile[], 
                     const char outputFile[]);


////////////////////////////////////////////////////////////////////////
// Function: lookupWord
//           Scans the dictionary file to see if the word in question
//           is present.
//
//  Inputs: a string representing the word in question
//          a string representing the filename of the dictionary
//
// Outputs: an integer representing whether the word was found
//          in the dictionary or not.
//          0 - word NOT found
//          1 - word found
//
int lookupWord(const char word[], const char dictFile[]);

////////////////////////////////////////////////////////////////////////
// Function: my_strupr
//           Converts a character string to all uppercase.
//
//  Inputs: a string representing the word to convert
//
// Outputs: the input string in all uppercase
//
void my_strupr(char string[]);



void main(void)
{
	char dictionary[MAX_FILENAME + 1];      // dictionary filename
	char inFileName[MAX_FILENAME + 1];      // input filename
	char outFileName[MAX_FILENAME + 1];     // output filename

		// get filenames from the user
	getFilenames(dictionary, inFileName, outFileName);

		// check words in input file against dictionary file
	spellCheckWords(dictionary, inFileName, outFileName);
}

void getFilenames(char dictionary[], char inputFile[], char outputFile[])
{
		// get dictionary filename
	cout << "Enter the name of the dictionary file" << endl;
	cin >> dictionary;

		// get input filename
	cout << "Enter the name of the file to check" << endl;
	cin >> inputFile;

		// get output filename
	cout << "Enter the name of the file to write the output" << endl;
	cin >> outputFile;
}

void spellCheckWords(const char dictionary[],
                     const char inputFile[],
                     const char outputFile[])
{

	char word[MAX_WORD + 1];  // this is the word in question
	int misspelled = 0;       // running total of misspelled words
	ifstream inFile;          // the input stream
	ofstream outFile;         // the output stream


		// open input file and check error
	inFile.open(inputFile, ios::in);
	assert( !inFile.fail() );

		// open output file and check error
	outFile.open(outputFile, ios::out);
	assert( !outFile.fail() );

		// prime while loop by reading a word from input file
	inFile >> word;

		// while there are more words in the input file
	while ( !inFile.eof() )
	{
			// lookup the word in the dictionary
		int found = lookupWord(word, dictionary);

			// if it was NOT in the dictionary
		if (found == 0)
		{
				// write it to the output file
			outFile << "Not found: " << word << endl;

				// increment count of misspelled words
			misspelled++;
		}
			// get next word in input file
		inFile >> word;
	}

		// output total number of misspelled words
	outFile << endl;
	outFile << "Number of words not found in dictionary: " << misspelled << endl;

		// close the input file
	inFile.close();

		// close the output file
	outFile.close();
}

int lookupWord(const char word[], const char dictionary[])
{
	char lexWord[MAX_WORD + 1];    // word from dictionary
	char upperLex[MAX_WORD + 1];   // dictionary word in uppercase
	char upperWord[MAX_WORD + 1];  // word in question in uppercase
	ifstream lexicon;

		// open dictionary and check for error
	lexicon.open(dictionary, ios::in);
	assert( !lexicon.fail() );

		// copy word into temp
	strcpy(upperWord, word);

		// convert copy of word to uppercase
	my_strupr(upperWord);

		// prime while loop by reading a word from the dictionary
	lexicon >> lexWord;

		// while there are more words in the dictionary
	while ( !lexicon.eof() )
	{
			// copy dictionary word to temp
		strcpy(upperLex, lexWord);

			// convert copy of dictionary word to uppercase
		my_strupr(upperLex);

			// if the word in question matches the dictionary word
		if ( strcmp(upperLex, upperWord) == 0 )
		{
				// close the dictionary
			lexicon.close();

				// return 1, meaning we found the word
			return 1;
		}

			// get next word in dictionary
		lexicon >> lexWord;
	}

		// close the dictionary
	lexicon.close();

		// if we get here, the word was not in the dictionary
	return 0;
}

void my_strupr(char string[])
{
	int i = 0;

		// convert all characters in string to uppercase
	while (string[i] != 0)
	{
		string[i] = toupper(string[i]);
		i++;
	}
}
Back to Outline