FILE WONT BE READ

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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>   
#include <fstream>   
#include <string>   
#include <set>   
#include <Windows.h>
#include <WinNT.h>
   


using namespace 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);   
            else if (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.
It's in the right location
Which file isn't opened? In the code above, there are two input files,
32
33
    // dictionary and document streams   
    ifstream dict, doc;  

The program statement to open doc is missing from the above program code.
LOL my file was called dictionary.txt not .bat stupid mistake

But now the program has another issue.

It just skips to this part
1
2
3
4
    // display the set of misspelled words   
    cout << endl << "Set of misspelled words" << endl;   
    cout << endl;
    system ("pause");

why wont it carry out the task of spell checking?
I would say it'd because of this:
58
59
60
61
62
63
    // 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.
okay so i addeddoc.open(filename.c_str()); but it still seems to execute the break any suggestions?
I'd add a check after the open() statement,
1
2
3
4
5
    if (!doc.is_open())
    {
        cout << "Error opening file: " << filename << endl;
        return;
    }
Last edited on
That didn't do any good Its as if the file is opening but nothing is being executed and/or read.
Please post your modified code.

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.
Last edited on
Topic archived. No new replies allowed.