Write a program that reads a string an outputs the number of timeseach lowercase vowel appears in it. Your program must contain afunction with one of its parameters a string, and return the numberof times each lowercase vowel appears in it. Also write a programto test your function. (Note that if str is a variable of typestring, then str.at(i) returns the character at the ith position.The position of the first character is 0.Also str.length() returns the length of the str, that is the number of characters in str.)
So, what is my problem? My code just won't even compile, giving me errors about undefined references and whatnot.
Note: I have to use void functions, so I must keep the one I have in the program. Also, I can't use arrays. Help me!
#include <iostream>
#include <string>
usingnamespace std;
void countVowels(string, char&);
int main()
{
string str;
cout << "Enter a string :" << endl;
cin >> str;
cout << "The number of a's:" << countVowels(str, 'a') << endl; //Function to count a's
cout << "The number of e's:" << countVowels(str, 'e') << endl; //Function to count e's
cout << "The number of i's:" << countVowels(str, 'i') << endl; //Function to count i's
cout << "The number of o's:" << countVowels(str, 'o') << endl; //Function to count o's
cout << "The number of u's:" << countVowels(str, 'u') << endl; //Function to count u's
system ("pause");
}
void countVowels(string str , char& ch)
{
int i, ch = 0;
for (i = 0; i < str.length(); i++) //Sets to 0 and sets length
if (str.at(i)==ch) ch++; //Returns to ith position (0)
}
Your countVowels is weird. In the first place, it should return an int. Since yours returns void, printing it does not makes sense. Second, you search for char 'a', and when you find it, you increase it, so as soon as you find an 'a', you continue searching for 'b', and when you find 'b' you search the rest of the string for 'c', and so on. The input parameter (ch) should not be the same name as ch on line 30. The thing that you define on line 30 should be the number of times you find ch, let's call that nch. So then you increase nch on line 33. And just before leaving the function, you return nch