Incorrect output in program

Hello, I am a beginner c++ code writer and basically my problem was, first input gives the number of lines, each line I must count the vowels used, and "y" counts as a vowel in this problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
#include <string.h>
using namespace std;
int main(){   
    int i=1;
    int vowel=0;
    int a;
    cin >>a; 
    for (int b=0; b<a; b++){
    string x; 
 getline(cin,x);
        if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u'|| x[i]=='y')
        vowel ++;
   cout<<vowel<<" "; }
}


Sample input:
17
jveb qsyaryxfvsemnuvir xi kbjaaxvy qvzwwqutuzn lcdhgotfy
t it ur chjcggahbuhvdzy mpcwtxrbu pyfcj sryes tocyo
jojsxfqqm dcznmhhf nkzbwtd ogpttqf ppfgbyplefi
thz pvpaiqg lbkvv lbm cgl jy zatxtltlmxbzr cquk
duirgjczzj xkcvgtrw qe jz spqcxogosrluqkmehu
hbifp kucjqoy dejq chrfebwcqaquhxy nadqc fztpi
gdm uxzdvcovzedxdemfhd bqx lnfvapj r ucuiyuck
n xuwlfnkmyewuymladz gbwkhzsxagm fpkvae
fmk sizs xm jfnhimhkyutv rwrykisallikdlhwgp qd
twdgi bzazvsygvz hklxti rjuey aodf hrn rndk q setdp es wcd
jqukohwjylwxybjaxv uqislcmr tucdsf t qfzc
xhk jhccla flqwdwrgyuylvaf gfvncfept ziyalkmb
weecd jvik rvl ltzpk dkxjljzergovlf s uwfcnbtcsp
j usrabckb b jey fznhvzqk msabojjib ekjnrwwgg
my ltth mxoyru lww iudmruknhvqauemwaar z
srv lhijbblm cpfxi pec r trxjrsbjnjai jgtaxpd abr wwkrnr
quejdne nb enby xjbxqzynwqgmhzzeoeng s ho pvsc

Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 3 3
Expected: 15 12 5 5 8 11 10 9 8 11 8 9 6 8 12 7 10
You need to check each character in the line you read from the string.
I'm not sure how to do that.... and also, how would the program where the end of the line is?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>	// not string.h, that's the C string, not C++ string

using namespace std;

int main()
{
	int vowel=0;

	int a;
	cin >> a;
	for (int b=0; b<a; b++)
	{
		string x; 
		getline(cin,x);

		for (size_t i = 0; i != x.size(); ++i)	// you have the index, but not the loop
			if (x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u'|| x[i]=='y')
				vowel ++;
		cout << vowel <<" ";
	}
}
Thanks a load for the help, man, that almost worked. The big problem is that now it adds upon the previous lines' vowels onto the new value.

Input: 17
izlmijxjyzdvjhwfgqnxheqwt njsz wxskubhm hvdwd bpxwngufd
lx fx bwgranymo hgls sjlpixsjbqo igyr iefeslrty qztjhgymfx
mni sjakiiphbhaady kvygfnrkbujdpihkvwk o
jvkjw piflcrwxj ymwkcaxswvddwx txwrsmtee
voughcxgsbgrod fnlcoziavvsjqw tsnxei ijagv
h stxuww w uqd wn mmmp qytuu lcadiz ownxrrnolczyqlscb
hjukinsezwigxoowssmtdt klgctnwtigx jmg l
rxbszemrrhomcmepvadsv y on hcxrowatvy nwvqkecp
epixllig ssour ip xmzg wbx gbuygsddfvhrj llddf zeglessoa
j xl ffxpqtihdfzvnc ucgh k mbdopc cnxbdpuhi sich jlqvz
g ezjjaes h mpqbregexk xwxnsxyyydep y msczgxpnqtav
ldbjb iayc f go mtzgezripg l pfxa b kca kghv vhcv tmkkz
ogo doorcvcmvuublzn wdbfettp yry ykc bmukex
yt swviuqzrlykr pbpvkcza xn e a h fdx owy
updkgm cujbuvyrkflfgzsyduiriawqi anzmtbpdjr pkqouo o
mgwpioqast jm dtundttafmo nhjjot qxhhzbc os hiokrgg gqyh
w dxhqp yig zhyywc o nzk hnnfbxdqnzi i

Output: 0 6 18 29 34 44 54 61 71 83 89 100 108 120 129 144 155
Expected: 6 12 11 5 10 10 7 10 12 6 11 8 12 9 15 11 7
How do I reset it for each line (or just minus the previous value)? I'm not sure what to do.... "break" doesn't seem to do the trick and "return" doesn't do me any good either.

I also noted that it was only from the third number onwards, so I attempted to "
int val=vowel
if (B>3) {cout << vowel-val;}
else {cout << vowel;}
But that also failed.
Last edited on
It counts all the vowels in the file, not in each line. But you ought to be able fix that.

You reset the count by assigning value zero when you process each line.
thanks. You should look at my other problem about tree height, it has the same 0 0 0 0 0 0 0.... output but it's math related instead. :D
Topic archived. No new replies allowed.