consonant counter

Hey guys. So I have this assignment:

Write a value-returning function, isConsonant that returns the value true if a given character is a consonant, and otherwise (if the character is a vowel) returns false. Write a main function that prompts the user to input a sequence of characters. You can use cin.get() to read one character at a time. Then call the function isConsonant for each character to count the consonants. For each letter, if isConsonant returns true, then add 1 to a total count value. After all consonants have been counted, print the total number of consonants. For design purposes, it will probably be easier to check if a character is not a vowel (a, e, I, o, u), rather than to check if it is a consonant.

When I input letters however, the output looks like this:

Enter a series of characters
john //my input
j o h n
the number of consonants is 4

The program isn't actually counting the consonants, and I don't really know what I did wrong. Any help will be appreciated, thanks.

this is my code
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
#include <iostream>
#include <string>

using namespace std;

bool isConsonant(char ch);

int main ( )
{
int consonants = 0;

string test;
test = "Enter a series of characters: ";
cout << test << endl;

getline (cin, test);


for (int i = 0; i < test.size ( ); i++)
{
if (isConsonant(test[i]))
{
cout << test[i] << " ";
consonants++;
}
}

cout << endl;

cout << "The number of consonants is " << consonants << " " << endl;
cin.get ( );
return 0;
}

bool isConsonant(char ch)
{
return (ch != 'a' || ch != 'e' || ch != 'i' || ch != 'o' || ch != 'u' );
}
Last edited on
closed account (48T7M4Gy)
Try && instead of ||
Thank you so much it works now!!!!!
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.