Hi, Im having this problem in which the program freezes, the the code in the function called doesn't execute. Help would be much appreciated.

// spellchecker2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<cmath>
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

bool inDictionary(string word, string dictionary[]);
void wordFilter(string& word);
void spell(string& filename);


//This function takes a file and spell checks it 
//@param file name
void spell(string& filename)
{
    ifstream file;
    string line,word,character;
    file.open(filename);

    while(!file.eof())
    {
        getline(file,line);

        for(int i=0;i<line.length(); i++)
         {
             //splitting off characters
             character= line.substr(i,1);
             if(character!=" ")
             {
                 word+=character;
             }
             else
             {
                 wordFilter(word);
                 word="";
             }

        }


    }
    return;

}

//This Function takes a word from input file and removes punctuations
//outputs word
//@param word that needs to be filtered
void wordFilter(string& word)
{
    string word2=word;
    string character;
    string out;
    ifstream source;
    source.open("dictionary.txt");
    string dictionary[197];
    string line;
    for(int i=0; i<197;i++)
    {
        getline(source,line);
        dictionary[i]=line;
    }

    for(int i=0; i< word2.length();i++)
    {
        character= word2.substr(i,1);
        if(character!="."||character!=","||character!=""""||character!="?"||character!="("|| character!=")")
        {
            out+=character;
        }

    }

    if(inDictionary(out,dictionary)==true)
    {
        cout<<out<<endl;
    }

    return;
}

//This function Returns True if the word is in the dictionary
//@param Strings needed to be checked
//@return True/False based on implemented algorithm
bool inDictionary(string word, string dictionary[])
{
    for(int i=0;i<197;i++)
    {
        if(word==dictionary[i])
        {
            return false;
        }

    }
    return true;
}

//This is the main Function
//@return Succesful termination of the program
int _tmain(int argc, _TCHAR* argv[])
{
    string filename;

    //file input
    cout<<"Please enter a file to be checked: ";
    cin>> filename;

    //calls function
    spell(filename);//does not execute
    system("Pause");
    return 0;
}

Additional thing is that I have to use this format as per assignment instuctions. The code should be working and i've spent hours trying to solve this problem. Thanks ahead of time.

Recommended Answers

All 3 Replies

The function is being called. I used VS 2012 to verify that. What did you enter for the filename? Why do you think that function is not being called? The program seems to freeze probably because there is an infinite loop. Use your compiler's debugger so that you can see exactly what is happening.

Yeah i figured that out, now the problem is that the file isn't actually opening causeing an infinite loop at

 while(!file.eof())

Fixed it, My compiler wasn't actually working properly, Any files that i added to the source weren't actually copied to the debug folder

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.