Word check and passing ostream objects

Hey guys,

I am working on a spell check program and I am trying to do things in a manner that is a little more extensive then the standard check word 'a' to the file 'filename.txt' in the following function I am first making sure that the word is actually in the form of a word. So a correct word is of the form 'letters only possibly followed by numbers or punctuation' incorrect eg. 'h3lp' or 'sk@te'. I am trying to check this using isalpha() but I am not having much success. Also there is an ostream object being passed in but I am not sure how to deal with it. I tried << to it then passing cout in the function call but I am not having any luck there either. Perhaps my code will clear things up. Please keep in mind that this code is in no way complete and there are some testing lines throughout.
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
bool bad_word(char word[], int line, ostream& out)
{
    cout << "here I am 6" << endl;
    int size = strlen(word) -1;
    char *p = word;
    bool checkWord[size];
    bool *chkWrd = checkWord;

cout << "here I am 6a" << endl;

    for(int i = 0; i < size; i++)
    {
        if(isalpha(*(p+i)))
           {
            *(chkWrd + i) = true;
           }

        else *(chkWrd + i) = false;
    }

cout << "here I am 7" << endl;

    for(int j = 0; j < size + 1; j++)
    {
        if(*(chkWrd + j) == false && *(chkWrd + j + 1) == true)
        {
            return true;
            j = size +2;
        }
        else return false;

    }

    cout << "here I am 8 " << endl;

    out << "Unknown word " << myWord << "found in line " << line << "of the input file" << endl;

    cout << "Unknown word " << myWord << "found in line " << line << "of the 
input file" << endl;
}

and the function call

bad_word(word, line, cout);

ANY help is appreciated, thanks in advance.
Last edited on
here is the whole thing if anyone wants it to compile or w/e

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
include <iostream>
#include <fstream>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

bool word_search(char word[], char dict[]);
bool bad_word(char word[], int line, ostream& out);

int main()
{
    string fileName;
    ifstream checkFile;
    string possibleWord = "";
    int length =0;
    int *lengthPtr = &length;
    char word[*lengthPtr];
    char *wrdPtr = word;
    int line = 0;

    int wordNum = 0;
    char dict[] = "sample_dictionary.txt";

    cout << "This program will check a given file for spelling errors" << endl;
    cout << "Please enter the name of the file that you would like to check" << endl;
    cin >> fileName;
cout << "here I am 1" << endl;
    checkFile.open(fileName.c_str());

    if(checkFile.fail())
    {
        cerr << "Error: Invalid File Name!!" << endl;
       // exit(0);
    }
cout << "here I am 2" << endl;
    //Writes in one word at a time from the file associated to 'fileName'
    // and puts it into 'possibleWord''
    // If 'possibleWord is a new line character we increment length by one to keep track of which line we are on.
    // Then we make sure that we indeed have a word, if so then we spell check said word. If not then we tell the user
    //that word 'such and such' is not a word and give them the line number on which this word occures.
  int i = 0;
    while(checkFile >> possibleWord)
    {
        wordNum += 1; //This keeps track of the number of words we have checked
        length = possibleWord.length();
        lengthPtr = &length;

            if(possibleWord.at(i) == '\n')
            line += 1;  // this keeps track of how many line numbes we have gone through.
            i++;

        bool badWord = bad_word(word, line, cout);
        if(badWord == true)
        {
            cout << "I am here special" << endl;
            bad_word(word, line, cout);
        }



        else
            word_search(word, dict);
    }
    cout << "here I am 3" << endl;
//word_search(word, dict);

}


bool word_search(char word[], char dict[])
{
    cout << "here I am 4" << endl;
    int size = strlen(word) -1;
    string myWord =word;
    int size2 = strlen(dict) - 1;
    char dictWord[100];

    ifstream dictionary(dict);

    while(dictionary >> dictWord)
    {
        if(dictWord == myWord)
        return true;

        else{
        cout << "The Word: " << myWord << " has not been recognized by spell check, Please be sure of the spelling of this word!" << endl;
        return false;
        }
    }

cout << "here I am 5" << endl;
}

bool bad_word(char word[], int line, ostream& out)
{
    cout << "here I am 6" << endl;
    int size = strlen(word) -1;
    char *p = word;
    string myWord = "";
    bool checkWord[size];
    bool *chkWrd = checkWord;
cout << "here I am 6a" << endl;
    for(int i = 0; i < size; i++)
    {
        //myWord.at(i) = *(p + i);
        if(isalpha(*(p+i)))
           {
            *(chkWrd + i) = true;
           }

        else *(chkWrd + i) = false;
    }
cout << "here I am 7" << endl;
    for(int j = 0; j < size + 1; j++)
    {
        cout << "7a1" << endl;
        if(*(chkWrd + j) == false && *(chkWrd + j + 1) == true)
        {
            cout << "7a2" << endl;
            return true;
            j = size +2;
        }
        else return false;

    }
    cout << "here I am 8 " << endl;
    out << "Unknown word " << myWord << "found in line " << line << "of the input file" << endl;
    cout << "Unknown word " << myWord << "found in line " << line << "of the input file" << endl;
}
Topic archived. No new replies allowed.