i need help.

i want to make a program in which program will read a string and display tot he screen count of the occurrance of each of the vowel in the string.
program is not runing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include<iostream>
#include<conio.h>
using namespace std;

int main()
{const  int size=50;
	char si[size];
	int count[5]={0};
	cout<<"pleas enter the numbers"<<endl;
	cin>>si[size];

	for(int i=0;i<size;i++)
	{
		if(si[i]=='a'||si[i]=='A')
			count[0]++;
		else if(si[i]=='i' || si[i]=='I')
			count[1]++;
		else if(si[2]=='o'|| si[i]=='O')
			count[3]++;
		else if(si[i]=='u' || si[i]=='U')
			count[4]++;
		cout<<si[i]<<endl;
		getch();
	}}
This is not the way to get the string from the user. cin>>si[size]; In fact it is accessing an element just outside the array which is not valid.

Instead try:
 
    cin.getline (si, size);

The for loop should use the actual length of the string entered by the user:
 
    for(int i=0; i<strlen(si); i++)
Last edited on
i am not using string..its just char array.
Quote:
i want to make a program in which program will read a string

When I say string here, I mean the same thing that you do, I'm talking about the character array which will contain the string input by the user.
but this is giving eror
cin.getline (si, size);
plz check the code i thnk its problm with count
In order to use strlen() you need to include the <cstring> header

Your original code had an error at line 18,
else if(si[2]=='o'|| si[i]=='O') -
si[2] should be si[i]

I made minimal changes to your code. But I made a new loop to output the results:
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
#include <iostream>
#include <cstring> // for strlen
#include <conio.h>

using namespace std;

int main()
{
    const  int size=50;
    char si[size];
    int count[5]={0};
    cout << "please enter the string" << endl;

    cin.getline (si, size);

    for (int i=0; i<strlen(si); i++)
    {
        if(si[i]=='a'||si[i]=='A')
            count[0]++;
        else if(si[i]=='i' || si[i]=='I')
            count[1]++;
        else if(si[i]=='o'|| si[i]=='O')
            count[3]++;
        else if(si[i]=='u' || si[i]=='U')
            count[4]++;
    }

    for (int i=0; i<5; i++)
    {
        cout << count[i] << endl;
    }

    getch();

}

Output:
please enter the string
The cat sat on the mat
3
0
0
1
0

Notice that the letter 'e' which occurs twice is not counted. That was an error in the original code, I leave that for you to fix.
Last edited on
Topic archived. No new replies allowed.