This if statement will always be true because you are assigning 'a' to the character variable.
Also, 'character' is uninitialized. What character are you checking?
Lastly, count being a parameter to vowelCount is worthless, because it will not change the 'count' variable in main.
Your vowelCount function doesn't really make much sense. It should probably return an integer that is the count, and should take the string as a parameter so it has something to actually count.
#include <iostream>
#include <string>
usingnamespace std;
void vowelCount(string count);
int main()
{
string sentence = "Testing this. Should have 9 vowels.";
//cout << "Please enter a sentence:" << endl;
//cin >> sentence;// Gets only word up to first space
// getline(cin, sentence); // Record whole sentence, including spaces
vowelCount(sentence);// Passes whole sentence to function
system("pause");
return 0;
}
void vowelCount(string sentence)
{
int count = 0;
for (int i = 0; i < sentence.length();i++)// loop through entire sentence here, in the fuction
if ((sentence[i] == 'a') || (sentence[i] == 'e') || (sentence[i] == 'i') || (sentence[i] == 'o') || (sentence[i] == 'u'))
// You weren't using comparison. single =, is assign, double ==, is compare
{
count++; // Increase count if vowel found
}
cout << "Vowel count : " << count << endl; // Show results
}
While whitenite's code will work, I want to reiterate that vowelCount should probably return the count and not output it to cout.
Functions should do 1 thing. A function named vowelCount logically should count vowels.
Counting and outputting are 2 different things, and vowelCount should not do both. It makes the function much less useful. How useful would sentence.length() be if it output the string length to cout instead of returning it to your code? It'd be completely worthless!
#include <iostream>
#include <string>
usingnamespace std;
int vowelCount(string count);
int main()
{
string sentence = "Testing this. Should have 9 vowels.";
int count;
//cout << "Please enter a sentence:" << endl;
//cin >> sentence;// Gets only word up to first space
// getline(cin, sentence); // Record whole sentence, including spaces
count = vowelCount(sentence);// Passes whole sentence to function
cout << "Vowel count : " << count << endl; // Show results
system("pause");
return 0;
}
int vowelCount(string sentence)
{
int vowels = 0;
for (int i = 0; i < sentence.length();i++)// loop through entire sentence here, in the fuction
if ((sentence[i] == 'a') || (sentence[i] == 'e') || (sentence[i] == 'i') || (sentence[i] == 'o') || (sentence[i] == 'u'))
// You weren't using comparison. single =, is assign, double ==, is compare
{
vowels++; // Increase vowel count if vowel is found
}
return vowels;
}
I didn't read your comment's very thoroughly. I wasn't realizing that getline(cin,sentence) gets input from the keyboard. A minute after I replied, it dawned on me. Thank you Whitenite (Your name fits you, btw)