I can't figure out what I have wrong in teh following program. I am trying to count total characters, vowels, and consonants. I have even worked through it with a friend and neither of us can figure it out. The code follows:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void stringout();
string input;
int main()
{
cout << "Input a string of characters: " ;
cin >> input;
cout << "You entered: " << input << endl;
stringout();
return 0;
}
void stringout()
{
char ch;
int vowel=0, consonant=0;
int length = input.length();
for (int i = 0; i < length; i++)
{
if ((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') ||
(ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U'))
++vowel;
else
++consonant;
}
cout << "This has " << input.length() << " characters, " <<
vowel << " vowels, and " << consonant << " consonants." << endl;
}
The program counts characters correctly, but it will not count vowels and counts every character as a consonant. I am thinking it has something to do with the ch variable not having any value so the program assumes that it every character is a consonant. I just can't figure out how to fix it.
Hopefully someone can help.