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 :)
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.