Spell Check Program Glitch (Please Help)
for my college course i have to write a program to spellcheck a file using a dictionary file (both read from user)
It should print a message when the word from the file is not in the dictionary.
Also, the program has to treat "dog" the same as "dog!!!" (meaning removing any non-alpbetic charactors from the end). But, of course, "d.o.g." shouldnt work.
My problem is this. Everything compiles, but when a word has non-alphabetic characters at the end, they are replaced with the '\0' character to remove them. I think this makes the word different from the one in the dictionary file because it has extra stuff at the end (sorry if that doesnt make sense).
So basically i get this as output:
(spellchecking a file containing the word "quiCK!!!" in line 1)
Unknown word "quick" found in line 1 of the input file.
even though the word "quick" IS in fact in the dictionary.
This program is due at midnight tonight and i cant figure it out.
Can anyone help me fix this glitch?
here is the code for my program(some of it may be considered "bad programming" but it works... sort of):
#include <iostream>
#include <cctype> //This library contains a file to convert to lower case
#include <fstream> // file stream
#include <cstdlib> // exit function
#include <sstream> // stringstream
#include <ctype.h> // isalpha function
using namespace std;
bool word_search(string word, string dict); //seaches dict for word
// returns true if it's there
void bad_word(const string word, const int line, ostream& out);
void removeNonAlpha(string &word); // removes non-alphabetic chars from end of word
void convertToLowerCase(string &word); //self-explanatory
int main()
{
string word, line, dict;
char file[20];
ifstream in_stream;
cout << "Enter name of file to spell check: ";
cin >> file;
in_stream.open(file);
if(in_stream.fail())
{
cout << "Failed to open file. Exiting Program. \n";
exit(1);
}
cout << "Enter name of dictionary file: ";
cin >> dict;
int lineNumber = 1;
while(getline(in_stream, line))
{
stringstream ss(line);
while(ss >> word)
{
removeNonAlpha(word);
convertToLowerCase(word);
if (!word_search(word, dict))
bad_word(word, lineNumber, cout);
}
lineNumber++;
}
}
bool word_search(string word, string dict)
{
string dictWord;
char dict2[20];
/////changes string dict to char array dict2
/////so it works with ifstream
for(int i = 0; i < 20; i++)
dict2[i] = dict[i];
ifstream in(dict2);
while(in >> dictWord) // gets word from dictionary file
{
if(dictWord == word) //checks if word is in //dictionary
{
in.close();
return true;
}
}
in.close();
return false;
}
void bad_word(const string word, const int line, ostream& out)
{
//error message
out << " Unknown word " << '"' << word << '"' << " found in line " << line << " of the input file. \n";
}
void removeNonAlpha(string &word)
{
string tmpWord = word;
int i = word.size();
while(!isalpha(word[i]))
{
tmpWord[i] = '\0';
i--;
}
word = tmpWord;
}
void convertToLowerCase(string &word)
{
int j = word.size();
for(int i = 0; i < j; i++)
word[i] = tolower(word[i]);
}

