Loop for counting single entered vowels hardship.

So I have a project due here soon.
It is on the topic of coding a program that allows the user to enter as many letters as they would like; one at a time.
I coded it, but I'm having trouble with the actual tally system itself not counting how many vowels are entered. It seems a mess in general, any help is definitely appreciated.

The commenting and labeling are required by the professor for reference.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  

// =================
#include "stdafx.h"
#include <iostream>

using namespace std;

// ============
// Prototypes:
// ============

void Banner();
bool IsVowel(char);
bool goAgain();
void GetChar(char&);
int  CounterVowel(int);
int  Print(int);


int main()
{
	Banner();
	do {
		char letter;
		int vowelcount = 0;
		int total = 0;
		total = CounterVowel(total);

		Print(vowelcount);


	} while (goAgain() == true);
	return 0;
} // Function Main()
  // ======================

  // =================
  // Function Banner()
  // Notes:
  // Void function. Displays
  // welcome screen for the
  // user.
  // =================
void Banner() {

	cout << "\n\nWelcome to the program!" << endl;
	cout << "I, the computer, am going to " << endl;
	cout << "ask you to input some letters." << endl;
	cout << "Then, I can tell you how many" << endl;
	cout << "vowels you entered." << endl;
	cout << "\nLet's start:" << endl;

} // Function Banner()
  // =======================


  // =======================
  // Function CounterVowel()
  // Notes:
  // Tracks how many vowels 
  // entered by user.
  // ======================
int CounterVowel(int total) {

	do {

		int vowelcount = 0;
		int total = 0;
		char letter;
		GetChar(letter);

		for(vowelcount = 0; vowelcount++;)

		if (IsVowel(letter)) {
			vowelcount++;
		}



	} while (goAgain() == true);

	return total;
}

// =============

// ==================
// Function goAgain()
// Notes:
// Creates the user loop.
// =======================
bool goAgain() {

	char answer;
	bool invalidAnswer;

	do {

		cout << "\nAnother letter? y/Y/n/N: " << endl;
		cin >> answer;

		if ((answer == 'n') || (answer == 'N') ||
			(answer == 'y') || (answer == 'Y'))
			invalidAnswer = false;
		else {
			invalidAnswer = true;
			cout << "You can't write that. Try a "
				"valid character" << endl;
		}
	} while (invalidAnswer);

	if ((answer == 'y') || (answer == 'Y'))
		return true;
	else if ((answer == 'n') || (answer == 'N'))
		return false;
}

// ==================
// Function GetChar()
// Notes:
// Gets user input
// ==================
void GetChar(char& letter) {

	cout << "Please enter a letter: ";
	cin >> letter;
}

// ==================
// Function IsVowel()
// Notes:
// Let's user know if 
// letter entered is a 
// vowel or not.
// ==================
bool IsVowel(char letter) {

	if ((letter == 'a') || (letter == 'A') ||
		(letter == 'e') || (letter == 'E') ||
		(letter == 'i') || (letter == 'I') ||
		(letter == 'o') || (letter == 'O') ||
		(letter == 'u') || (letter == 'U'))
		return true;

	else
		return false;
}

// ======================
// Function Print()
// Notes:
// Prints vowels entered
// by the user.
// =======================
int Print(int vowelcount) {

	cout << vowelcount << " vowels found.";
	return 0;
}
You do have unused variables in multiple places. Get rid of them.

The logic should be simple:
count = 0
char input
WHILE read input
  IF input is vowel THEN ++count

print count
I slightly edited the coding a bit for the counter, but I still have issues with it. He wanted it set up like this:




int CounterVowel(int vowelcount) {

do {

int vowelcount = 0;
char letter;
GetChar(letter);

if (IsVowel(letter) == true) {
vowelcount++;
}
else
cout << Print(vowelcount);


} while (goAgain() == true);

return 0;
}





with the coding that I have now, here is a sample output of what it creates:



Welcome to the program!
I, the computer, am going to
ask you to input some letters.
Then, I can tell you how many
vowels you entered.

Let's start:
Please enter a letter: a

Another letter? y/Y/n/N:
n
0 vowels found.
Another letter? y/Y/n/N:
n
Press any key to continue . . .
Last edited on
1
2
3
4
int CounterVowel(int vowelcount) {
  // code
  return 0;
}

What is the point of returning an int that is always 0? What information does it deliver to the caller?

1
2
3
4
5
6
void CounterVowel( int vowelcount ) {
  do {
    int vowelcount = 0;
    // code
  } while ( goAgain() == true );
}

The local variable vowelcount (line 3) hides the function argument vowelcount (line 1) in the scope of the loop. The argument is not used.
1
2
3
4
5
6
void CounterVowel() {
  do {
    int vowelcount = 0;
    // code
  } while ( goAgain() == true );
}

The goAgain() returns a bool. The goAgain() == true is true, if and only if goAgain() is true.
1
2
3
4
5
6
void CounterVowel() {
  do {
    int vowelcount = 0;
    // code
  } while ( goAgain() );
}

Same applies to the condition with IsVowel(letter) in it.

1
2
3
4
void GetChar( char& );

char letter;
GetChar(letter);

Seems akward. Can't you use:
1
2
3
char GetChar();

char letter = GetChar();



Inside loop:
1
2
3
4
5
6
7
8
if ( IsVowel(letter) )
{
  vowelcount++;
}
else
{
  cout << Print(vowelcount);
}

In other words:
IF user gives a vowel, then nothing is shown.
ELSE (not a vowel), then count is shown.

What if the input is "aaaaaaaaaa"? Nothing will be shown, because they all are vowels.


Oh, it gets better:
1
2
3
4
do {
  int vowelcount = 0;
  cout << Print(vowelcount);
} while ...

The vowelcount is 0 at start of each iteration. Only non-vowels print, so the only possible output is 0.

IMHO, the program should either show count after each input or show count only once after all input. Your choice.
1
2
3
4
5
6
7
8
9
int vowelcount = 0;
do {
  if ( something ) {
    ++vowelcount;
  }

  cout << vowelcount;  // either after each letter
} while ( goAgain() );
cout << vowelcount;  // or only once after all letters 


1
2
3
4
5
6
int Print(int foo) {
	cout << foo << " vowels found.";
	return 0;
}

cout << Print(vowelcount);

does the same as:
cout << vowelcount << " vowels found." << 0;
I edited the counter.
As:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void CounterVowel(int vowelcount) {

	
	do {
		
		char letter;
		GetChar(letter);

		if ( IsVowel(letter) ) {
			vowelcount++;
		}

	} while (goAgain() == true);
	cout << "Results: ";
	Print(vowelcount);
	
}



my results:





Welcome to the program!
I, the computer, am going to
ask you to input some letters.
Then, I can tell you how many
vowels you entered.

Let's start:
Please enter a letter: a

Another letter? y/Y/n/N:
y
Please enter a letter: g

Another letter? y/Y/n/N:
n
Results: 1 vowels found.
Another letter? y/Y/n/N:


I will go and practice it more. Thank you so much for letting me pick your brain!
I think both your and user's life will be much easier if you try to grab all the characters typed until <Enter> key (hence the check against '\n') ;P

You also want to be careful to eat the rest of the line after the user decides to repeat or not -- he might enter "yNyNyNasdfasa" or something, and we're only grabbing a single character. The newline from user's hit of <Enter> key is still in play and could inadvertently be checked.

I've also added an alternate way to check vowel using a "fall-through" technique with switch statement, where break isn't needed because all paths lead to a return.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <limits>

using namespace std;

void Banner() 
{

    cout << "\n\nWelcome to the program!" << endl;
    cout << "I, the computer, am going to " << endl;
    cout << "ask you to input some letters." << endl;
    cout << "Then, I can tell you how many" << endl;
    cout << "vowels you entered. Let's start!" << endl;
}

int VowelCount(char c)
{
    switch(c)
    {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            return 1;
        default:
            return 0;
    }
}

int main() 
{
    Banner();
    char answer;
    char letter;
    int count;
    do
    {
        cout << "\nInput a bunch of letters and press <Enter>\n";
        cout << "? ";
        count = 0;
        while (cin.get(letter) && letter != '\n')
        {
            count += VowelCount(letter);
        }
        cout << "Total vowels: " << count << endl << endl;
        cout << "Again (y/n)? ";
        cin >> answer;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    while (answer=='y' || answer=='Y');
    
    cout << "\nBye!" << endl;
    return 0;
}


can test at https://repl.it/repls/DrabSvelteComputergames

Welcome to the program!
I, the computer, am going to 
ask you to input some letters.
Then, I can tell you how many
vowels you entered. Let's start!

Input a bunch of letters and press <Enter>
?  Heyyyyyy, how's it going?! 
Total vowels: 5

Again (y/n)?  y

Input a bunch of letters and press <Enter>
?  A bird in the hand is worth a hundred in the air.
Total vowels: 14

Again (y/n)?  n

Bye!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool isVowel( char c )       // adapt for your language!
{
   const string vowels = "aeiouAEIOU";
   return vowels.find( c ) != string::npos;
}

int main()
{
   int totalVowels = 0;
   string line;
   cout << "Enter some lines of text; blank line to end:\n";
   while ( getline( cin, line ) && line != "" ) totalVowels += count_if( line.begin(), line.end(), isVowel );
   cout << "That's " << totalVowels << " vowels in all";
}



Enter some lines of text; blank line to end:
Now is the winter of our discontent ...
Summer isn't looking much better either!

That's 23 vowels in all
Topic archived. No new replies allowed.