counting vowels

Oct 4, 2016 at 8:25pm
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;

}
Oct 4, 2016 at 8:37pm
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.
Oct 4, 2016 at 8:42pm
closed account (LA48b7Xj)
change

 
count = count++;


to

 
count++;
Last edited on Oct 4, 2016 at 8:43pm
Oct 4, 2016 at 8:52pm
Oct 4, 2016 at 9:29pm
Ok thankes.
What the difference between (count = count++;) to (count++;) ?
Oct 5, 2016 at 6:56am
closed account (LA48b7Xj)
count = count++; is undefined behaviour.
Oct 5, 2016 at 8:35am
"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 Oct 5, 2016 at 8:36am
Oct 5, 2016 at 1:33pm
Topic archived. No new replies allowed.