Spell Check.

So this program has to do the following:

- Enter a word
- Open a predetermined text file (it is assumed that it's always there, in this case it's dict.txt)
- If the word exists in the text file it will display the word and a message stating it's correct. (ex. word is spelled correctly)
- If the word does not exist or is spelled incorrectly it will assume it's wrong and displays a message stating it's incorrect.
- finally if we type "exit" the program ends.

I can make it find the word and display correctly but I can't get done the other two. I've been trying for days and checked every resource I can think of but just can't find what the problem is. (pretty sure been sick is not helping)

The code I have so far is as it follow:

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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
    ifstream dictionary("dict.txt");
    char word[50];
    int array_size = 1024;
	char * array = new char[array_size];
	int position = 0;

    cout << "Please enter the word to  search in file : " << endl;
    cin.getline(word,49);
    int word_size = 0;

    if(dictionary.is_open())
	{
    	while(!dictionary.eof() && position < array_size)
		{
			dictionary.get(array[position]);
			position++;
		}
		array[position-1] = '\0';

    if (word == "exit")
    {
        cout << "ending program..." << endl;
        return 0;
    }
    {
    for(int i = 0; array[i] != '\0'; i++)
		{
            for(int j = 0; word[j] != '\0' && j < 20 ; j++)
            {
            if(array[i] != word[j])
            {
            break;
            }
            else
            {
            i++;
            if(word[j+1] == '\0')
            {
            cout << word << " is spelled correctly." << endl;
            }
            }
            }
		}
	}
	return 0;
}
}


Any assistance or pointers to the right direction would be greatly appreciated.
word == "exit" ← You cannot compare c-strings like that. Use strcmp() to compare or std::string to hold your words.

If after all words in dictionary are compared with your word, no match is found you should output "incorrect word" message
Topic archived. No new replies allowed.