Won't count number of vowels

Program runs but does not count the number of vowels.

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>
using namespace std; 


bool isVowel(char x);

int main()
{

 string phrase;
 char ch;
 bool x;
 int counter=0;   
    
    cout << "Enter a series of characters: "; 
    cin.get(ch);


for (int i = 0; i < phrase.length(); i++)
{
 x = isVowel(phrase[i]); 
 if (x == true)
{
counter++;
}
}
cout << " There are " << counter << " vowels in " << phrase << endl;
system("pause");
}

bool isVowel(char x)
{
   if ((x == 'a') || (x == 'A') || (x == 'e') || (x == 'E') || (x == 'i')
   || (x == 'I') || (x == 'o') || (x == 'O') || (x == 'u') || (x == 'U'))
   	return 1;
   else
	return 0;
}
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
#include <iostream>
#include <string>
using namespace std; 


bool isVowel(char x);

int main()
{
    string phrase;
    bool flag;
    int counter = 0;   
    
    cout << "Enter a series of characters: "; 
    getline(cin, phrase);


    for (int i = 0; i < phrase.length(); i++)
    {
        flag = isVowel(phrase[i]); 
        
        if (flag)
            counter++;
    }
    
    cout << "There are " << counter << " vowels in " << phrase << endl;
    
    system("pause");
    
    return 0;
}

bool isVowel(char x)
{
    x = toupper(x);
    
   if ((x == 'A') || (x == 'E') || (x == 'I') || (x == 'O')|| (x == 'U')|| (x == 'Y'))
   	return true;
   else
	return false;
}


_You can't get a "series of characters" in a single char, however you can use getline to give you a string.
_You weren't doing anything anyway with the char ch after you got input from user
_You can use toupper to have a smaller if statement, also you forgot the vowel 'y' :)

Hope this helps
Last edited on
As a mini-project I got curious about doing this in other ways, with the result below:

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

bool isVowel(char x)
{
    string vowels="AEIOU";
    size_t found = vowels.find(x);
    if (found!=string::npos) return true;

return false;
}

int main()
{
    string phrase;
    int counter = 0;   
    
    cout << "Enter some characters: "; 
    getline(cin, phrase);

    for (char c : phrase) 
        counter += (isVowel(toupper(c)));
  
    cout << "There are " << counter << " vowels in " << phrase << endl;

return 0;
}
Last edited on
Different yet interesting approach indeed @Cheddar99
Greatly appreciated. Thanks
Topic archived. No new replies allowed.