Need help creating a program that outputs and counts the vowels in a sentence.

I have been stuck on this program for almost a week and cant seem to find out what's wrong with it. I would appreciate it if you helped me with my code and not write a different one (since usually its far more advanced to me to even understand).

Thank you in advance!

As for the program it has to get the sentence entered, output all the vowels and then say how many vowels the sentence had.

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<cctype>

using namespace std;

int main()
{
	int iterations = 0;
	int numVowels = 0;
	char vowels;

	cout << "Enter a sentence: ";

	while (iterations == 10);
	{
		cin.get(vowels);

		switch (toupper(vowels))
		{
		case 'A':
		case 'E':
		case 'I':
		case 'O':
		case 'U':
			numVowels++;
			cout << vowels << ",";
			iterations++;
		}
	}

		cout << "\n\nVowels: " << vowels << endl << endl;
		cout << "There are " << numVowels << " vowels in this sentence.\n\n";

	return 0;
}
while (iterations == 10);

This does not do what you expect.

While loops run for as long as the given condition is true. So this means that your loop will keep looping only while iterations == 10.

Since you initialize it with 0 iterations = 0;, that means it will not be 10... and your loop body will never run.

ALSO -- that semicolon after the while statement doesn't belong there. It effectively ends the while loop completely so that it has no body. The following {block} of code is not considered part of the while statement.


So yeah... you probably wanted this:
1
2
	while (iterations < 10) // <- loop while iterations is less than 10.  NO SEMICOLON
	{
line 8 you initialize iterations to 0 then on line 14 you loop only if iterations is equal to 10???

It looks to me like you are trying to input a sentence of 10 characters 1 character at a time then after each character is inputted you are increasing the count and the iterations. In that case it would have to be != 10 ( loop 10 times ).

Here is how I would do it.
1) Input a full sentence.
2) iterator through the sentence
3) if vowel found while iterating increase the count by 1.


Also are you trying to put all the vowels into a string or something? What are line 10 and 31 doing?



Example of how I would do it:

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>

int main()
{
    using std::cout;
    using std::cin;
    using std::getline; //I assume you want spaces in your sentences
    using std::string; // can you use std::string? Or only char arrays/chars?
    using std::endl;

    
    string setence , vowels = "";
    int numVowels = 0;

    cout << "Please enter a setence: ";
    getline( cin , setence );

    for( int i = 0; setence[i]; ++i ) //loop while it is not a null character
    {
        switch( toupper(setence[i]) )
        {
            case 'A': case 'E': case 'I': case 'O': case 'U': //some times y?
            ++numVowels;
            vowels += setence[i];
        }
    }

    cout << "Number of vowels: " << numVowels << endl;
    cout << "Vowels used are: " << vowels << endl;
}


http://ideone.com/OO8m6A
for( int i = 0; setence[i]; ++i )

std::string, when accessed with the [] operator, is not necessarily null terminated. Null is just another character and has no special significance. It's only significant with char arrays.

When working with std::string, you really should check the length() or size() rather than look for the null.
Ah sorry for some reason I was thinking that getilne appended the null terminator at the end of strings and char arrays.
Topic archived. No new replies allowed.