Vowel Counter Program Problem

Hi I am very new to C++ so I may not be able to find the problem in my program very accurately. I have made a program which calculates the number of vowels in a user inputted string. The problem is in the for loop. The string TotalVow gives me the length of the user inputted string and not just the vowels. Any help or suggestions would be greatly appreciated! Here it is:

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

using namespace std;

int main() {

	string input;
	string result;

	int numA, numE, numI, numO, numU, totalVow;
	totalVow = 0;

	cout << "Enter a string you would like me to count the vowels in: ";
	getline(cin, input);

	int vowelCount[5] = { numA = 0, numE = 0, numI = 0, numO = 0, numU = 0 };
	cout << endl;
	string vowelNames[5] = { "A: ", "E: ", "I: ", "O: ", "U: " };

	for (int i = 0; i < input.length(); i++) {

		if (input[i] = 'a' || 'A') {
			numA += 1;
		}
		else if (input[i] = 'e' || 'E') {
			numE += 1;
		}
		else if (input[i] = 'i' || 'I') {
			numI += 1;
		}
		else if (input[i] = 'o' || 'O') {
			numO += 1;
		}
		else if (input[i] = 'u' || 'U') {
			numU += 1;
		}
	}
	totalVow += numA + numE + numI + numO + numU;

	cout << "You entered " << totalVow << " vowels!" << endl << "Here are the number of hits per vowel: " << endl;

	for (int i = 0; i < 5; i++) {
		cout << vowelNames[i] << vowelCount[i] << endl;
	}
}


Your if statements are wrong for two reasons.

The first reason is you are using = instead of == to compare. The second reason is, when you are using logical or || you are not putting the full condition after the or...

The first one for example
 
if (input[i] = 'a' || 'A')


Should be

 
if (input[i] == 'a' || input[i] == 'A')




Last edited on
Thanks, it works now!
Topic archived. No new replies allowed.