Help on hwk Assignment

For my hwk, I have to count the number of vowels, constants, capitals, and lowercase letters in a string of characters. I have to make 4 functions for this code and it is suppose to count in there.
So it reads the string, but when I run it, it looks like it only reads one of the character from the string. So my question for this is how do I read all the characters from the string that gets read in and counts the vowels, constants, etc.?
Thanks in advance.

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
# include <iostream>
# include <string>
# include <vector>
using namespace std;
// Function prototypes - declaration without a body
int countVowels(string);
int countConstants(string);
int countCapitals(string);
int countLowercase(string);

int main() {
	string input;
	vector <string> stringChar;
	cout << "Please enter a string of characters: ";
	getline(cin, input);
	stringChar.push_back(input);

	cout << countVowels(input) << " vowels\n" << countConstants(input)
		<< " constants\n" << countCapitals(input) << " capital letter\n"
		<< countLowercase(input) << " lowercase letter" << endl;

	system("pause");
	return 0;
}
// Function definition - where function is implemented beneath the int main()
int countVowels(string phrase) {
	int count = 0;
	for (auto singleChar : phrase) {
		if (singleChar == 'a' || singleChar == 'A' || singleChar == 'e'
			|| singleChar == 'E' || singleChar == 'i' || singleChar == 'I'
			|| singleChar == 'u' || singleChar == 'U') {
			count++;
		}
		return count; // Stops function and returns answer
	}
}

int countConstants(string phrase) {
	int count = 0;
	for (auto singleChar : phrase) {
		if (singleChar >= 66 && singleChar <= 68 || singleChar >= 70
			&& singleChar <= 72 || singleChar >= 74 && singleChar <= 78
			|| singleChar >= 80 && singleChar <= 84 || singleChar >= 86
			&& singleChar <= 90 || singleChar >= 98 && singleChar <= 100
			|| singleChar >= 102 && singleChar <= 104 || singleChar >= 106
			&& singleChar <= 110 || singleChar >= 112 && singleChar <= 116
			|| singleChar >= 118 && singleChar <= 122) {
			count++;
		}
		return count;
	}
}

int countCapitals(string phrase) {
	int count = 0;
	for (auto singleChar : phrase) {
		if (singleChar >= 65 && singleChar <= 90) {
			count++;
		}
		return count;
	}
}

int countLowercase(string phrase) {
	int count = 0;
	for (auto singleChar : phrase) {
		if (singleChar >= 97 && singleChar <= 122) {
			count++;
		}
		return count;
	}
}
Last edited on
Where are your return statements?
At the end of each count* function?
Inside loops?

Your program loops through the string four times. That is ok, but there is an alternative design.
One loop that calls all isVowel(char), isCapital(char), etc functions.

There is a third stye too:
http://www.cplusplus.com/reference/algorithm/count_if/
The std::count_if is a function that does loop, but does call function like isVowel.


You have met the requirement "write four functions". Could you use library functions within?
For example, something from:
http://www.cplusplus.com/reference/cctype/
Topic archived. No new replies allowed.