Hello, I'm working on the following exercise but this is as far as I got. Here is a picture of what happens when I run it: https://puu.sh/sa8Nk/f7cde65740.png. It incorrectly says the sentence has only 1 character, and doesn't display the vowels like it should.
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
void analyze(char a, int &numberVowels)
{
if(!isspace(a))
cout << a << " - ";
if (
tolower(a) == 'a' || tolower(a) == 'i' || tolower(a) == 'u' || tolower(a) == 'e' || tolower(a) == 'o' ||
toupper(a) == 'A' || toupper(a) == 'I' || toupper(a) == 'U' || toupper(a) == 'E' || toupper(a) == 'O'
)
{
cout << "Vowel";
numberVowels++;
}
else
{
if(!isspace(a))
cout << "Consonant";
}
cout << endl;
}
int main()
{
int i;
int numberVowels = 0;
string sentence;
cout << "Please input a sentence : ";
getline(cin, sentence);
cout << "Number of letters in this sentence: " << sentence.length() << endl;
for (i = 0; i < sentence.length(); i++) analyze(sentence[i], numberVowels);
cout << endl;
cout << numberVowels << " characters are vowel(s)." << endl;
cin.get();
return 0;
}
Please input a sentence : This is a cat
Number of letters in this sentence: 13
T - Consonant
h - Consonant
i - Vowel
s - Consonant
i - Vowel
s - Consonant
a - Vowel
c - Consonant
a - Vowel
t - Consonant
4 characters are vowel(s).