Using Functions to count Vowels/Consonants QUICK LOOK PLEASE

In this program, I am suppose to input a string, then have a menu that I can input A, B, C, D, E. A is suppose to be a function that counts the vowels within the string. B is suppose to be a function that counts the consonants. C. is suppose to display both functions. D. is suppose to let you input a new string. And E. is suppose to just exit the program. I am having trouble with the pointers with the functions, vowCounter and conCounter. If anyone can quickly look at this and tell me what is wrong. My visual basic will not debug it if I choose A, B, or C.

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

using namespace std;

int* vowCounter(string, int);
int* conCounter(string, int);

int main()
{
	//Definition of str, as well as the menu display
	string str,
	       a = "A) Count the number of vowels",
	       b = "B) Count the number of consonants",
	       c = "C) Count both the vowels and consonants",
	       d = "D) Enter another string instead",
	       e = "E) Exit the program";
	char choice;
	int vowelC = 0;
	int consonC = 0;
	int* vptr = 0; 
	int* cptr = 0;

	do
	{	
		cout << endl;
		cout << "Enter a string: ";
		cin >> str;

		cout << endl;
		cout << "What do you wish to do in the string? " << endl;
		cout << a << endl;
		cout << b << endl;
		cout << c << endl;
		cout << d << endl;
		cout << e << endl;
		cin >> choice;

	
		{
			switch (toupper(choice))
			{
			case 'A':
			{
				cout << *vptr;
				cout << endl;
				break;
			}
			case 'B':
			{
				cout << *cptr;
				cout << endl;
				break;
			}
			case 'C':
			{
				cout << *vptr;
				cout << endl;
				cout << *cptr;
				cout << endl;
				break;
			}
			case 'D':
			{
				continue;
				break;
			}
			case 'E':
			{
				cout << "Exiting the program....." << endl;
				break;
			}
			default:
			{
				cout << "Input is invalid, try again. ";
				cout << endl;
				break;
			}
			}
		}
	} while (toupper(choice) != 'E');

	return 0;
}

int* vowCounter(string str, int vowelC)
{
	int* vptr = new int[vowelC];

	for (int i = 0; i < str.length(); i++)
	{
		int temp = toupper(str[i]);

		if (temp == 'A', 'E', 'I', 'O', 'U')
		{
			vowelC++;
		}
		else
		{
			cout << "There are no vowels. " << endl;
		}
	}
		return vptr;
}

int* conCounter(string str, int consonC)
{
		int* cptr = new int[consonC];

		for (int i = 0; i < str.length(); i++)
		{
			int temp = toupper(str[i]);
			if (temp != 'A', 'E', 'I', 'O', 'U')
			{
				consonC++;
			}
			else
			{
				cout << "There are no consonants. " << endl;
			}
		}
		cout << "Total consonants are " << consonC << endl;

		return cptr;
}

Last edited on
You never call the functions vowCounter or conCounter in main. Also, I am completely confused about the logic that is inside those functions. What is vowelC, where do you initialize it, what are you supposed to store in the vptr array?
Hi,

The 2 functions are almost the same, can you figure out how to do it with one? You will need to pass arguments to the function for the counters by reference, so you can use the values in main().

Line 95 is wrong, look at this example:

1
2
3
if (temp == 'a' || temp== 'b' || temp == 'c) {
   //code
} 


With the switch, try assigning a value of true to a bool quit variable, then use this as the end condition for a while loop.


Also, try putting the display of the menu into it's own function. Assigning those strings to variables is not really worth it for this.

The pointer variables point to an array of ints, which isn't useful. Just use std::string

Good luck :+)
Thanks for the help guys, I am still having trouble with the functions and pointers. My switch statements are not what is the issue, it is mainly my functions. I need one to count the consonants in a string, and one that counts the vowels in a string. vowelC and consonC are suppose to be the counters. I am having a difficult time with the pointers.
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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

string* vowCounter(string, int);
string* conCounter(string, int);

int main()
{
	//Definition of str, as well as the menu display
	string str;
	char choice;
	int vowelC = 0;
	int consonC = 0;
	string* vptr = vowCounter(str, vowelC);
	string* cptr = conCounter(str, consonC);

	do
	{	
		cout << endl;
		cout << "Enter a string: ";
		cin >> str;

		cout << endl;
		cout << "What do you wish to do in the string? " << endl;
		cout << "A) Count the number of vowels" << endl;
		cout << "B) Count the number of consonants" << endl;
		cout << "C) Count both the vowels and consonants" << endl;
		cout << "D) Enter another string instead" << endl;
		cout << "E) Exit the program" << endl;
		cin >> choice;

	
		{
			switch (toupper(choice))
			{
			case 'A':
			{
				cout << *vptr;
				cout << endl;
				break;
			}
			case 'B':
			{
				cout << *cptr;
				cout << endl;
				break;
			}
			case 'C':
			{
				cout << *vptr;
				cout << endl;
				cout << *cptr;
				cout << endl;
				break;
			}
			case 'D':
			{
				continue;
				break;
			}
			case 'E':
			{
				cout << "Exiting the program....." << endl;
				break;
			}
			default:
			{
				cout << "Input is invalid, try again. ";
				cout << endl;
				break;
			}
			}
		}
	} while (toupper(choice) != 'E');

	return 0;
}

string* vowCounter(string str, int vowelC)
{
	string* vptr = 0;

	for (int i = 0; i < str.length(); i++)
	{
		int temp = toupper(str[i]);

		if (temp == 'A' || temp =='E' || temp == 'I' || temp == 'O' || temp == 'U')
		{
			vowelC++;
		}
		else
		{
			cout << "There are no vowels. " << endl;
		}
	}
		return vptr;
}

string* conCounter(string str, int consonC)
{
	string* cptr = 0;

		for (int i = 0; i < str.length(); i++)
		{
			int temp = toupper(str[i]);
			if (temp != 'A' && temp != 'E' && temp != 'I' && temp !='O' && temp != 'U')
			{
				consonC++;
			}
			else
			{
				cout << "There are no consonants. " << endl;
			}
		}
		cout << "Total consonants are " << consonC << endl;

		return cptr;
}
Hi,

Was it part of the assignment to specifically have 2 functions, 1 for vowels and 1 for consonants?

If so, that is a shame because it kind of reinforces bad practice. After all, if it is not a vowel, then it's a consonant: hence my comment about writing one function. The big clue there is to put something else into the else statement.

Your switch statement is part of the problem, you still don't call your functions. Functions don't execute just because one defines them, they only do so when called. Read the tutorial, on this site - at the top left of this page.

Why are you using pointers to strings? You don't do anything with them. Are you sure they weren't meant to be C-style pointer to array of char?

Your for loops execute lines 96 and 115 multiple times, is that what you want?

The do loop will only execute once because of the end condition, hence my suggestion of a bool quit variable. Search on this site for bool controlled while loop.

Maybe you should post the assignment in full ?

Hope all this helps, I look forward to seeing some big improvements in your code.
Bloody Hell, I just noticed that this is an almost duplicate topic.

It's a bit annoying, I spent all this time replying to this topic, only to discover you had a working program in the other one. This why we don't like duplicate topics.

I suppose the only mitigating thing is that some of my suggestions still apply.

As LB said, get a better book. Go for one of the Giants of the language - Stroustrup, Herb Sutter, Andrei Alexandrescu, Scott Meyers.

If you want to learn C++ then learn C++, if you want to learn C, then learn C - try not to mix the two unless you really need to.
Topic archived. No new replies allowed.