Help fixing an infinite loop

I'm getting an infinite loop. My code seems right to me so I can't figure out how to fix the infinite loop

A file name is passed to the function. The point of the function is to cout all of the words in string filename that don't appear in my dictionary files "A.txt, B.txt, etc"

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
  bool Dictionary::SpellChecking(string filename)
{
  ifstream fin;
  ifstream din;
  string word=" ";
  string dictname="#.txt";
  string dictword;
  fin.open(filename.data());
  ofstream fout;
  if(fin.fail())
    return(Dictionary::failure);
  else
    {
      cout << "These are the misspelled words: " << endl;
      cout << "***************************" << endl;
      while(!fin.eof())
        {
          fin >> word;
          dictname[0]=toupper(word[0]);
          din.open(dictname.data());
          while(!din.eof() || word!=dictword)
            {
              din >> dictword;
            }
                if(word!=dictword)
                  cout << word;
        }
      cout << "***************************" << endl;
      //infinite loop. this ^ never shows
      return(Dictionary::success);
    }
}
closed account (o3hC5Di1)
Hi there,

You should probably check whether "din" successfully opened.
If it didn't, it's failbit will be set, meaning its eof bit won't be set and your while loop keeps going as a result.

Alternatively you could do:

1
2
3
4
5
6
din.open(dictname.data());
          while(din >> dictword)
            {
              if (dictword != word)
                  break;
            }


Hope that helps, please do let us know if you require any further help.

All the best,
NwN
line 21 looks like a possible reason...

in order for that loop to stop din must be at eof and word must = dictword.

are those conditions being met?
Topic archived. No new replies allowed.