Counting vowels from input stream

Hi, trying to write a helper function to count the amount of each vowel present in a given text file. This is a textbook exercise, not homework; just trying to understand what I'm doing wrong. Not worried about whitespace, so I'm just using the >> operator. I'm mostly there but I'm getting the wrong output. Does anyone see where I went wrong here?

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

int main()
{
   string vowels = "aeiou";
   int counters[] = {0, 0, 0, 0, 0};

   cout << "Input file: " << endl;
   string input_file_name;
   cin >> input_file_name;
   
   ifstream in_file;
   in_file.open(input_file_name);
   if(in_file.fail()) { return -1; }
   
   char ch;
   while(in_file >> ch)
   {
      
      for(int i = 0; i < vowels.length(); i++)
      {
         if(ch == vowels[i])
         {
            counters[i]++;
         }
      }
   }
   
   
   for (int i = 0; i < vowels.length(); i++)
   {
      cout << vowels[i] << ": " << counters[i] << endl;
   }
   
   return 0;
}


This is what the autograder is telling me:


Test 1

Actual output

Input file:
a: 131
e: 180
i: 92
o: 136
u: 48
Expected Output

Input file:
a: 133
e: 182
i: 105
o: 136
u: 48
Since you are under-counting, I suspect you are simply not allowing for the fact that sentences and proper nouns start with a capital letter.

What happens if you put
ch = tolower( ch );
as line 23? (You may need #include <cctype> at the start.)

And you're obviously not in Wales. Or Denmark.
Last edited on
Seems ok, does the input have a mix of upper and lower case characters?
Unsure what the input even is, since it isn't ever shown with the autograder. I will try converting the chars to lower before looping through the file to count.

Wales or Denmark? lol no I'm in the US.
#include <cctype> is already there
Yep, I tried adding ch = tolower(ch); and that did the trick! Thanks!
Topic archived. No new replies allowed.