Whats wrong with my switch case?

Hello,
I begun my journey into C++ and programming this week and I have been able to make some simple programs. I have this homework assignment I cannot solve right now. This code should take in city name and count vowels in it. For some reason, it counts wrong. It seems to count one extra 'u' in every time even if word don't have a single 'u' in it. It's working as intended if I comment out the 'u' case.

I'm using Dev-C++ version 4.9.9.2.

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
  #include <iostream>
using namespace std;
int main()
{
int vowel=0;
    char testi[20];
    cout << "Insert city name: ";
    cin >> testi;
//    cout << "You entered: " << testi << "\n";

    for(int k=0;k<20;k++)
    switch (testi[k])
    {
           case 'A':
           case 'a':
           case 'E':
           case 'e':
           case 'I':
           case 'i':
           case 'O':
           case 'o':
           case 'U':
           case 'u': //this is causing my problems, don't know why
           case 'Y': //y is vowel in finnish
           case 'y':
              vowel++; break;
                default: break;
                }
    
    cout << "Vokaalien lkm: " << vowel;
    
cout << "\nPaina <ENTER> lopettaaksesi...";
cin.sync(); //helpotetaan ohjelman käyttöä Dev-C++ ohjelmassa
cin.get();
return 0;
}


This could be alternative way, but it counts all characters in string?

1
2
3
for(int k=0;k<20;k++)
    if(kaupunki[k] == ('A'||'a'||'E'||'e'||'I'||'i'||'O'||'o'||'U'||'u')) 
    vowel++;


Any ideas?
char testi[20] {0};

You left the array uninitialized. This should fix it.
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
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int vowel=0;
    char testi[20];
    cout << "Insert city name: ";
    cin >> testi;
//    cout << "You entered: " << testi << "\n";

    for( size_t k=0, n = strlen( testi ); k < n; k++ )
    switch (testi[k])
    {
           case 'A':
           case 'a':
           case 'E':
           case 'e':
           case 'I':
           case 'i':
           case 'O':
           case 'o':
           case 'U':
           case 'u': //this is causing my problems, don't know why
           case 'Y': //y is vowel in finnish
           case 'y':
              vowel++; break;
                default: break;
                }
Thank you Vlad!
I got it working. Had to define number of loops with length of my string.
Topic archived. No new replies allowed.