So i've got this program that should prompt the user to input a filename to which the program spell checks it. Anywho...here's the code..
For some odd reason I can't get the bloody file to be read.
Please help
Any and all help is appreciated
Thanks
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <Windows.h>
#include <WinNT.h>
usingnamespace std;
void spellChecker(string& filename);
int main()
{
string fileName;
cout << "Enter the document to spell check: ";
cin >> fileName;
// check the spelling
spellChecker(fileName);
return 0;
}
void spellChecker(string& filename)
{
// sets storing the dictionary and the misspelled words
set<string> dictionary, misspelledWords;
// dictionary and document streams
ifstream dict, doc;
string word;
char response;
// open "dict.dat"
dict.open("dictionary.dat");
if (!dict)
{
cerr << "Cannot open \"dict.dat\"" << endl;
exit(1);
}
// open the document file
// insert each word from the file "dict.dat" into the dictionary set
while(true)
{
dict >> word;
if (!dict)
break;
// insert into the dictionary
dictionary.insert(word);
}
// read the document word by word and check spelling
while(true)
{
doc >> word;
if (!doc)
break;
// lookup word up in the dictionary. if not present
// assume word is misspelled. prompt user to add or ignore word
if (dictionary.find(word) == dictionary.end())
{
cout << word << endl;
cout << " 'a' (add) 'i' (ignore) 'm' (misspelled) " ;
cin >> response;
// if response is 'a' add to dictionary; otherwise add to the
// set of misspelled words
if (response == 'a')
dictionary.insert(word);
elseif (response == 'm')
misspelledWords.insert(word);
}
}
// display the set of misspelled words
cout << endl << "Set of misspelled words" << endl;
cout << endl;
}
Check the location of your file "dictionary.dat".
It needs to be either
in the same folder (or sub-directory) as your executable program
or
in the current working directory for the program.
// read the document word by word and check spelling
while(true)
{
doc >> word;
if (!doc)
break;
Since the stream doc has not been opened (as I previously pointed out), the input at line 61 will fail, and so the break statement at line 63 is executed.
Are you using a debugger to step through the code line by line?
If not, then I'd recommend adding extra cout statements before and after the line doc >> word; to display the contents of "word" and also the status of the file. cout << "A. word: " << word << " doc: " << doc.good() << endl;
if you have multiple messages, use a unique letter such as A, B, C etc. to identify which message it is.