Need help with this C++ exercise. Counting how many vowels in a sentence

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.

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
42
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

string analyze(char a)
{
	if (a == 'a', 'e', 'i', 'o', 'u')
	{
		cout << a << endl;
		int numberVowels;
		return "Vowel";
		numberVowels++;
	}
	else
	{
		cout << " " << endl;
		return "Consonant";
	}
}

void main()
{
	int numberVowels;
	string sentence;
	cout << "Please input a sentence: ";
	sentence = cin.get();
	int numberLetters = sentence.length();
	cout << "Number of letters in this sentence: " << sentence.length() << endl;
	srand(time(0));
	for (int i = 0; i < numberLetters; i++)
	{
		sentence[i];
		string output = analyze(sentence[i]);
		if (output == "Vowel")
			cout << sentence.c_str() << endl;
		else
			cout << " " << endl;
	}
}
Last edited on
1.
if (a == 'a', 'e', 'i', 'o', 'u')
It should be :
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')

2.
1
2
3
int numberVowels;
return "Vowel";
numberVowels++;

What do you mean?

3.
void main()

Should be :
int main()
Last edited on
2. I mean that if the character is equal to any of those letters, it should add to the number of vowels.

But the problem is this code isn't even working to display the numbers of letters in the sentence (so far).

Sorry for not clarifying, the code is supposed to display the number of vowels in the sentence as well.
Last edited on
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
42
43
44
45
46
#include <iostream>
#include <string>
#include <cctype>

using namespace 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).


http://cpp.sh/6dk4o
Last edited on
Thanks a ton. Can you explain your usage of isspace since I had not learned about the <cctype> header previously? Would be appreciated
http://www.cplusplus.com/reference/cctype/isspace/
Topic archived. No new replies allowed.