counting vowels

I wrote a program which counting vowels, but the problem is when I enter a sentence it's kinda mixed up the program, but one I put words it works fine, so what do I do wrong here?
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
  #include <iostream>


using std::cout;
using std::cin;
using std::endl;

const int  size = 150;

int main()
{	
	int i = 0;
	int count = 0;
	char x [size];

	cout << "Enter string"<< endl ;
	cin.getline (x, size);
	while (x[i] != '\0')
	{	
		 x[i] ;
		i++;
	}
	

	cout << "The number of vowels are : " << endl;
	for (i = 0; i < x[i]; i++)
	{	
	
			if ( (x[i] == 'a') || (x[i] == 'o') || (x[i] == 'u') ||(x[i] == 'i') ||(x[i] == 'y') ||(x[i] == 'e')  )
			{
				count = count++;
			}
		
	}
		
	cout << count << endl;
		
	return 0;

}
Line 20: What do you think this statement does? Hint: it does nothing.

Line 26: Why are you using x[i] as the upper limit of your loop?

You're counting the number of characters using our while loop at line 18, but you overlay the count (i) at line 26.
closed account (LA48b7Xj)
change

 
count = count++;


to

 
count++;
Last edited on
Ok thankes.
What the difference between (count = count++;) to (count++;) ?
closed account (LA48b7Xj)
count = count++; is undefined behaviour.
"y" isn't a vowel
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
#include <iostream>
using namespace std;



int main()
{	
	string  x;
	int count = 0;
	
	
	
	cout << "Enter string"<< endl ;
	getline (cin, x);
	

	
	cout << "The number of vowels are : " << endl;
	for (int i = 0; i < x.length(); i++)
	{	
	
			if ( (x[i] == 'a') || (x[i] == 'o') || (x[i] == 'u') ||(x[i] == 'i') ||(x[i] == 'e')  )
			{
				 count=count+1;
			
			}
			
	}cout << count << endl;
		

		
	return 0;

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