DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Spell Check Program Glitch (Please Help)

  1. #1
    Join Date
    Dec 2005
    Posts
    15

    Spell Check Program Glitch (Please Help)

    hi,
    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]);
    }

  2. #2
    Join Date
    Dec 2005
    Posts
    15
    if its needed, here are my "dictionary" and test files

    dictionary.txt
    brown
    dog
    fox
    jumped
    lazy
    over
    quick
    the

    testfile.txt

    the quiCK!!! brown FOx j.umped
    oVer? the L,A.Z.Y dog!!!
    the qUICK BrOWN Fox JUMPED over the LaZy DOG
    the;
    the666
    the?

    pretty basic, but they do the job for testing.

  3. #3
    Join Date
    Feb 2006
    Posts
    25
    the problem is that the '\0' are making a difference, to fix this it changed '\0' to ' '(one space)then used this trim function if found on codeproject
    Code:
    void trim2(string& str)
    {
        string::size_type pos = str.find_last_not_of(' ');
        if(pos != string::npos) {
          str.erase(pos + 1);
          pos = str.find_first_not_of(' ');
          if(pos != string::npos) 
    		  str.erase(0, pos);
        }
        else str.erase(str.begin(), str.end());
    }
    so modify your removeNonAlpha function to look like:
    Code:
    void removeNonAlpha(string &word)
    {
    	string tmpWord = word;
    	int i = (int)word.size();
    	while(!isalpha(word[i]))
    	{
    		tmpWord[i] = ' '; //replace with spaces
    		i--;
    	}
    
    	trim2(tmpWord);
    	word = tmpWord;
    }
    and it should work...at least it does on my computer

  4. #4
    Join Date
    Dec 2005
    Posts
    15
    thanks, that works

    unfortunatly its 14 minutes late :(

    oh well. I'm going to re-submit it and see if I get a better grade with part that working.

Similar Threads

  1. Help with a program
    By stormswimmer in forum Java
    Replies: 2
    Last Post: 01-02-2006, 04:17 PM
  2. Replies: 7
    Last Post: 08-04-2005, 07:28 PM
  3. processing additional data when program is running
    By Ervin Rodriguez in forum Database
    Replies: 1
    Last Post: 05-16-2003, 08:45 AM
  4. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 10:24 AM
  5. Check box isssue when bound to a database column
    By Narayan in forum VB Classic
    Replies: 0
    Last Post: 04-10-2001, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center