Searching a dict file for words with certain #of vowels, consonants and first letter

This program is supposed to search for and output certain words (or numbers) based on how many vowels, consonants, and the first letter (or number) of that word/number. I've been working on this for a few days now and just can't figure out the problem. I've searched for similar problems/solutions through google and this forum, but still can't get it. I'm thinking there's a problem with either the while or for loop.

The program runs without errors, and will find words in the dictionary file like "AAA" when I input 0 consonants, 3 vowels, and starts with A, but it won't find the word "zip" if I input 2 consonants, 1 vowel, and starts with z. Any help/hints would of course be greatly 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
     1  #include <iostream>
     2  #include <fstream> //required for file reading
     3  #include <string>
     4  using namespace std;
     5
     6  int main()
     7  {
     8  int NumVowels;
     9  int NumCons;
    10  char FirstChar;
    11  int vowelCount = 0;
    12  int consCount = 0;
    13
    14  cout << "Enter consonants: ";
    15  cin >> NumCons;
    16  cout << "Enter vowels: ";
    17  cin >> NumVowels;
    18  cout << "Enter first character: ";
    19  cin >> FirstChar;
    20
    21    string value;
    22    ifstream inFile;
    23    inFile.open("/usr/share/lib/dict/words");
    24    if (inFile.fail())     // ensure that no error occurred
    25    {
    26      cout << "Error opening file - quit\n";
    27      return 1;
    28    }
    29          // no error - read every record in file
    30
    31  while (getline(inFile, value))
    32  {
    33     int len = value.length();
    34    if (value.at(0) != FirstChar)
    35          continue;
    36
    37       for (int i = 0; i < len; i++)
    38    {
    39       if (isalpha(value[i]))
    40       {
    41        if (value[i] == 'A' ||
    42            value[i] == 'a' ||
    43            value[i] == 'E' ||
    44            value[i] == 'e' ||
    45            value[i] == 'I' ||
    46            value[i] == 'i' ||
    47            value[i] == 'O' ||
    48            value[i] == 'o' ||
    49            value[i] == 'U' ||
    50            value[i] == 'u') vowelCount++;
    51        else consCount++;
    52        }
    53      }
    54      if (vowelCount == NumVowels && consCount == NumCons)
    55      {
    56       cout << value << endl;
    57       cout << vowelCount << endl;
    58       cout << consCount << endl;
    59       cout << len << endl;
    60  
    61       vowelCount = 0;
    62       consCount = 0;
    63      }
    64    }
    65  
    66  
    67      inFile.close();
    68    cout << "PROGRAM ENDS\n" << endl;
    69    return 0;
    70  }
Last edited on
1
2
3
4
5
6
7
8
9
10
    54  if (vowelCount == NumVowels && consCount == NumCons)
    55      {
    56       cout << value << endl;
    57       cout << vowelCount << endl;
    58       cout << consCount << endl;
    59       cout << len << endl;
    60  
    61       vowelCount = 0; // **** move this outside the if
    62       consCount = 0; // **** move this outside the if
    63      }


You wouldn't get into this kind of problem if you define variables closest to their point of use, within the smallest scope in which they are required.

1
2
3
4
5
6
7
8
9
10
11
    31  while (getline(inFile, value))while (getline(inFile, value))
    32  {
    33        int len = value.length();
    34        if (value.at(0) != FirstChar)
    35          continue;

                int vowelCount = 0; // ******
                int consCount = 0;   // ******
    36    
    37       for (int i = 0; i < len; i++)
               // ... 

Thank you very much, that got it working :) I knew I needed to reset the counts, but I was thinking it needed to be at the end, not at the beginning of the loop. Thanks again.
Topic archived. No new replies allowed.