Hopefully a quick fix

My program is not just pulling the vowels. If you know what I'm missing or doing incorrectly, can you please reply with help.

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
36
37
  //This program counts the number of lowercase vowels in a string

#include <iostream>
#include <string>


using namespace std;

void vowelCount(int count);

int main()
{
	string sentance = "Test";
	int count = 0;
	int i;

	//cout << "Please enter a sentence:" << endl;
	//cin >> sentance;

	for (i = 0; i < sentance.length (); i++)
		vowelCount(count );

	system("pause");
	return 0;
}

void vowelCount(int count = 0)
{
	char character;	
	
		if ((character = 'a') || (character = 'e') || (character = 'i') || (character = 'o') || (character = 'u'))
		{
			count++;
			cout << "Vowel count: " << count << endl;
		}

}
if ((character = 'a') || (character = 'e') || (character = 'i') || (character = 'o') || (character = 'u'))


= is assignment
== is comparison

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.
@Sarmonroe
You should just pass the entire sentence to the function, and count the vowels there.
Here's your code, tweaked.

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
#include <iostream>
#include <string>

using namespace 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!
You are correct Disch. I should have done it this way, to be more programming correct.
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
#include <iostream>
#include <string>

using namespace 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;
}
Thank you all for your feedback. I got it to work and understand everything you all said. I really appreciate you guys taking the time.
Last edited on
Comment out cin >> sentence; You only need one input command. I had both shown, but commented out, to show you the difference in what they do.
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)

Topic archived. No new replies allowed.