dictionary

I am trying to find incorrect words in an input file. I am having an issue with spaces, new lines, and punctuation. Any help or input is appreciated.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;

const int Error = 1; // Global variable

//-----------------------------------------------------------------------------

int hashFunction(const string &key, int tableSize)
{
 int hashVal = 0;

 for(int i = 0; i < key.length();  i++) 
 hashVal = 37 * hashVal + key[i];
 hashVal %= tableSize;

 // Handle any negative values 
 // Won't happen with strings or ASCII values, 
 // could happen with other data types
 if(hashVal < 0)
  {
   hashVal += tableSize;
  }

return hashVal;
}

//-----------------------------------------------------------------------------

int main()
{

ifstream dictionary_File("dictionary.txt", ios::in); // Open dictionary

if(dictionary_File.is_open()) // Error check opening file
 {
  cout << "\nDictionary file open." << endl;
 }
else
 {
  cout << "\nDictionary file did not open. Exiting." << endl;
  return(Error);
 }

//-----------------------------------------------------------------------------

ifstream check_File("check.me", ios::in); // Open check file

if(check_File.is_open()) // Error check opening file
 {
  cout << "\nCheck file open." << endl;
 }
else
 {
  cout << "\nCheck file did not open. Exiting." << endl;
  return(Error);
 }

//-----------------------------------------------------------------------------

const int tableSize = 101111; // Prime number
string line;
string *hashTable = new string[tableSize]; // Allocate hashTable

//-----------------------------------------------------------------------------

for(int i=0; i<tableSize; i++) hashTable[i] = -1; // Set hashTable to NULL
  
//-----------------------------------------------------------------------------

while(getline(dictionary_File, line)) // Hash Dictionary
 {
  hashTable[hashFunction(line,tableSize)] = line; // Input into HashTable
 }
cout << "\nDictionary in hashTable" << endl;

dictionary_File.close(); // Close filestream

//-----------------------------------------------------------------------------

//line.compare(hashTable[hashFunction(line,tableSize)]) != 0) 
//line != hashTable[hashFunction(line, tableSize)]
string word;
//istringstream iss;
while(getline(check_File, line, ' ')) // Obtain words with space delimiter
 {
  //iss.clear();
  //iss.str(line);
  if(line != hashTable[hashFunction(line,tableSize)]) // Compare w dictionary
   {
    word = line; // Save word as original
    for(int i=0; i < line.length(); i++) // Set all to lower case
	 line[i] = tolower(line[i]);
    cout << "This is the new line after to lower: " << line << endl;

    for(int i=0; i < line.length(); i++)
    if(ispunct(line[i])) line[i] = ' ';
    cout << "This is the new line after is punct: " << line << endl;

    if(line != hashTable[hashFunction(line,tableSize)])
     {
      cout << "\nWord: '" << line << "' is misspelled." << endl;
     }
    else continue; // Spelled correctly
   }
  else continue; // Spelled correctly
     
 }// end while

check_File.close(); // Close filestream
//-----------------------------------------------------------------------------

cin.get();
cin.ignore(); 
return (0); // Success
}// end main

//----------------------------------------------------------------------------- 



C:\Users\Zero>g++ HashProblem.cpp

C:\Users\Zero>a.exe

Dictionary file open.

Check file open.

Dictionary in hashTable
This is the new line after to lower: this
This is the new line after is punct: this
This is the new line after to lower: a
This is the new line after is punct: a

Word: 'a' is misspelled.
This is the new line after to lower: and
This is the new line after is punct: and

Word: 'and' is misspelled.
This is the new line after to lower: ducks
This is the new line after is punct: ducks

Word: 'ducks' is misspelled.
This is the new line after to lower: john.
This is the new line after is punct: john

Word: 'john ' is misspelled.
This is the new line after to lower: where
This is the new line after is punct: where
This is the new line after to lower: oh
where
This is the new line after is punct: oh
where

Word: 'oh
where' is misspelled.
This is the new line after to lower: horses
This is the new line after is punct: horses

Word: 'horses' is misspelled.
This is the new line after to lower: moon
This is the new line after is punct: moon

Word: 'moon' is misspelled.
This is the new line after to lower:
is
This is the new line after is punct:
is

Word: '
is' is misspelled.
This is the new line after to lower: about
This is the new line after is punct: about

Word: 'about' is misspelled.
This is the new line after to lower: he
This is the new line after is punct: he

Word: 'he' is misspelled.
This is the new line after to lower: a
This is the new line after is punct: a

Word: 'a' is misspelled.
This is the new line after to lower: barking.


This is the new line after is punct: barking



Word: 'barking

' is misspelled.

C:\Users\Zero>


I'm not sure why it is saying that words like 'a' are misspelled, they are within the dictionary.

Any ideas?
My apologies, at first glace I was mistaken. I'm afraid I cannot help you at this moment, though. I will look at it the first chance I get if no one else has.
Last edited on
Which section of code are you talking about? line is a string
Any help is appreciated, it has to be in my comparison. I just don't know what it could be though.
Topic archived. No new replies allowed.