Vowel Counting Program Problems

closed account (2hfG1hU5)
I am making a program that is supposed to check how many vowels are in a string of characters, it works fine except when a space is included, in these cases it only counts how many vowels are before the space then it breaks the loop and closes the program

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
38
39
40
 
#include<iostream>

using namespace std;

int main()
{
	
	cout << "Hello and welcome to my vowel counting program!" << endl;

	char Cont = 'n';
		do
		{
			cout << endl << "Please enter a string of characters: ";
			unsigned char Counted[31] = { '\0' };
			cin >> Counted;

			int VowelsTotal = 0;

			for (int Rounds = 0; Rounds < 30; Rounds++)
			{
				if (Counted[Rounds] == 'a' || Counted[Rounds] == 'A' || Counted[Rounds] == 'e' \
					|| Counted[Rounds] == 'E' || Counted[Rounds] == 'i' || Counted[Rounds] == 'I' || Counted[Rounds] == 'o' \
					|| Counted[Rounds] == 'O' || Counted[Rounds] == 'u' || Counted[Rounds] == 'U')
				{
					VowelsTotal++;
				}
			}

			cout << endl << "There are " << VowelsTotal << " vowel(s) in that string!" << endl << endl;


			cout << "Would you like to go again? (y/n): ";

			cin >> Cont;

		} while (Cont == 'y');

		return 0;
	}
This only reads in one word and stops at a white space.
cin >> Counted;
You need to use getline() if you want to input more than one word.

Is there any reason you do not just use string instead of char[]?
closed account (2hfG1hU5)
Does string allow me to check each individual letter like in my if statement in line 22?
Mixing unformatted input and formatted input requires a little care:

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

int main()
{
    const std::string vowels = "aeiouAEIOU" ;

    std::cout << "Hello and welcome to my vowel counting program!\n" ;

    char Cont = 'n';
    do
    {
        std::cout << "\nPlease enter a string of characters: ";

        // http://www.mochima.com/tutorials/strings.html
        std::string Counted ;

        // http://www.cplusplus.com/reference/string/string/getline/
        std::getline( std::cin, Counted ) ; // unformatted input: does not skip leading white space

        int VowelsTotal = 0;

        for ( char c : Counted ) // http://www.stroustrup.com/C++11FAQ.html#for
        {
            // http://www.cplusplus.com/reference/string/string/find/
            if( vowels.find(c) != std::string::npos ) ++VowelsTotal ;
        }
        std::cout << "\nThere are " << VowelsTotal << " vowel(s) in that string!\n\n" ;

        std::cout << "Would you like to go again? (y/n): ";
        std::cin >> Cont; // leaves trailing white space (new line) in the input buffer

        // http://www.cplusplus.com/reference/istream/istream/ignore/
        std::cin.ignore( 100, '\n' ) ; // throw away the trailing white space (new line)

    } while ( Cont == 'y' || Cont == 'Y' );

}
A string is an array of characters. Each character has an index just as an array does.

For example:
1
2
for(int i = 0; i < some_string.length(); i++)
    some_string[i] = 'a';

will change every character in the string to a, including white spaces.

Last edited on
closed account (2hfG1hU5)
Thanks! Works beautifully now! Plus I learned a bit which is always a bonus.
Topic archived. No new replies allowed.