Hello all, I have this program where I need to read in a letter or short paragraph and then spell check it with a dictionary file (a small one first for testing). I figured the best way to do this (at my knowledge level) is to read in both files to arrays, convert the one to be checked to lower case, strcmp each element and then output any word that is not found in the dictionary.
The problem is with the second data readin. Reading in the dictionary file works just fine, but for some reason, the second file reads in nothing, it just skips it. I switch the for loop parameters to read it until a certain size and it just reads in whitespace I guess, it just shows "" "" "" in the variable array list. When I put the code for the dictionary second, THAT one doesn't get read in and the other one does. I can't get my finger on what is wrong with the code that the second read in doesn't work.
I have searched around quite a bit and haven't been able to find a solution to this issue, any help/guidance would be greatly appreciated.
Here's the code. It's a work in progress, but if I could just get both files to read into arrays I can get the rest worked out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void makeLower (char word[]);
void linearSearch (char dictionary[][30], int size, char word[][30]);
int main ()
{
char filename[30];
char word[300][30];
char dictionary[300][30];
ifstream fin;
fin.open ("dictionary.txt");
if (fin.fail())
{
cout << "FILE NOT FOUND\n";
exit (0);
}
for (int index=0; !fin.eof(); index++) //read in dictionary file one //word by word
{ //already alphabetized.
fin.getline (dictionary[index],30);
}
fin.close ();
cout << "Enter the file name: ";
cin >> filename;
fin.open (filename);
if (fin.fail())
{
cout << "FILE NOT FOUND\n";
exit (0);
}
for (int index2=0; !fin.eof(); index2++) //this is the file to be spell checked
{
fin >> word[index2]; //WHY WILL THIS NOT READ ANYTHING IN!?!?
makeLower (word[index2]); //to get all words to lower case
}
fin.close ();
linearSearch (dictionary, 300, word);
//just a simple linear search for now.
return 0;
}
void makeLower (char word[])
{
int length = strlen(word);
for (int n=0; n < length; n++)
word[n] = tolower(word[n]);
}
void linearSearch (char dictionary[][30], int size, char word[][30])
{
bool found = false;
for (int scan=0; scan < size; scan++)
{
for (int n=0; n < size && !found; n++)
{
if (strcmp(dictionary[n], word[scan]) ==0)
found = true;
}
if (!found) //this section still needs work but ok for now
cout << endl << "Misspelled word list: \n\n" << word[scan] << endl;
}
}
|
Ah I see, thanks for the tip...now it should be more readable, sorry about that. And, I still haven't figured this bug out.