Creating a user defined function to count number of vowels

The output I'm getting here just counts every letter in the sentence and counts them as vowels. I'm trying to make the user defined function return the amount of vowels within the sentence.

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
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cctype>
using namespace std;


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 true;}
	else{return false;}	
}

int main()
{
	
	int vowel = 0;
	int uppercase = 0;
	int lowercase = 0;
	char sentence;

	cout << "Enter sentence: ";
	cin.get(sentence);

	while(sentence != '\n')
	{
		if(isupper(sentence))// >= 'A' && sentence <= 'Z')
		{
			uppercase++;
		}
		if(islower(sentence))// >= 'a' && sentence <= 'z')
		{
			lowercase++;
		}
		if(isvowel(sentence));
		{
			vowel++;
		}

		cin.get(sentence);
	}

	cout << setfill('.');
	cout << "Number of uppercase letters." << right << setw(4) <<uppercase << endl;
	cout << "Number of lowercase letters." << right << setw(15) << lowercase << endl;
	cout << "Number of vowels." << right << setw(15) << vowel << endl;

	return 0;
}
Semicolon on line 41
Wow, thankyou!
Also, going by line 26, I take it you just want to use single chars??

Andy
Last edited on

Also, going by line 26, I take it just want to use single chars??


Probably better to add:

 
#include <string> 


and then read the whole sentence somthing like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;

const int MAX = 100;

int main()
{
   string sentence;
   getline(cin, sentence);
   cin.ignore(MAX, '\n');
}


HTH
Last edited on
Topic archived. No new replies allowed.