Write a program that reads a string and outputs the number of times each lowercase vowel appears in it.

closed account (DG6DjE8b)
What i am doing wrong..

#include <iostream>
#include <string>

using namespace std;

int vowelsNumberof (string, char);

int main()
{
string str;

cout << "Enter a string :" << endl;
cin >> str;

cout << "The number of a's:" << vowelsNumberof (str, 'a') << endl;
cout << "The number of e's:" << vowelsNumberof (str, 'e') << endl;

cout << "The number of i's:" << vowelsNumberof (str, 'i') << endl;
cout << "The number of o's:" << vowelsNumberof (str, 'o') << endl;

cout << "The number of u's:" << vowelsNumberof (str, 'u') << endl;
}

int vowelsNumbersof(string str)

{
int i, count = 0;

for(i=0; i < str.length(); i++) //Sets to 0 and sets length
if(str.at(i)==count) count++; //Returns to ith position (0)

return count;
}
Please use code tags.

http://www.cplusplus.com/articles/jEywvCM9/

From what I can see, you've prototyped

int vowelsNumberof(string, char)

1.) Name these arguments i.e (string word, char vowel)
2.) You've defined the function in the wrong manner

1
2
3
4
5
6
7
8
9
int vowelsNumbersof(string str) 
{
   int i, count = 0; 

   for(i=0; i < str.length(); i++) //Sets to 0 and sets length
      if(str.at(i)==count) count++; //Returns to ith position (0)

   return count;
}


You're only passing in the word, and not the vowel you need check for in the first place. You're also not using your if-statement correctly. You should be checking str.at(i) == vowel not the count. If that comparison is true then you'll increase count.

1
2
3
4
5
6
7
8
9
10
int vowelsNumbersof(string str, char vowel) 
{
	int i, count = 0; 

	for(i=0; i < str.length(); i++) //Sets to 0 and sets length
		if(str.at(i) == vowel) 
			count++; //Returns to ith position (0)

	return count;
}


Topic archived. No new replies allowed.