counting Consonants function

I am attempting to write a function that counts the number of consonants in a string the user inputs.
My output is a question mark with a box around it.
I have looked over my code line by line, and the logic seems correct to me.

If anyone could help that would be greatly appreciated.

All necessary preprocessors are included in my program, I have just not added them here.

Also I am not allowed to use any other library functions such as isvowel.

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
// main function- function call;

int main()
{
	string input;
	int selection;
	cout << "Please enter a word, a sentence or a string of numbers: " << endl;
	getline(cin, input);
	input = countConsonants(input);
	cout << input << endl;

// function body;
int countConsonants(string input)
{
	
	char ch;
	int num = 0;
	for (int x = 1; x < input.length(); x++)
	{
		ch = tolower(input[x]);
		if (isalpha(ch && (ch != 'a' || ch != 'e' || ch != 'i' || ch != 'o' || ch != 'u'))
			num = num + 1;
	}
	return num;
	
	cout << "The String has " << num << " Consonants." << endl;
}
Last edited on
Some quick logic error :
1
2
if (isalpha(ch) && (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u'))
			num = num + 1; 
Last edited on
Does that help you? :)
@closed: It did not fix the problem, but I do understand the error I made in the logic.
So your program is still running incorrectly? What parts of your program are still going wrong?

Anyway, update your new code and give us some more details about your new problem.
The program compiles without any errors. But the output when user selects 5 and then 7 outputs a ? inside a box rather than an int value of how many consonants are within the string.

The steps that it should go through are as follows:

1) User inputs a string they want to manipulate
2) User selects option from menu based on what they want to do to their string
3) Based on selection, program jumps to switch statement and executes the case that reflects the user selection
4) In order to display the result of the executed case, user must select 7
5) User keeps selecting from menu or pushes 0 to quit program.

Main ()
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
int main()
{
	string input, result;
	int selection;
	cout << "Please enter a word, a sentence or a string of numbers: " << endl;
	getline(cin, input);
	do
	{
			cout << endl;
			cout << "USE THIS MENU TO MANIPULATE YOUR STRING " << endl;
			cout << "----------------------------------------" << endl;
			cout << "1) Inverse String" << endl;
			cout << "2) Reverse String" << endl;
			cout << "3) To Uppercase" << endl;
			cout << "4) Count Number Words" << endl;
			cout << "5) Count Consonants" << endl; // Up to here require functions
			cout << "6) Enter a Different String" << endl;
			cout << "7) Print the String" << endl;
			cout << "0) Quit" << endl;
			cin >> selection;
			cout << endl;
		
			switch (selection)
			{
				case 1:
					 input = inverseString(input);
					cout << endl;
					break;
				case 2:
					input = reverseString(input);
					cout << endl;
					break;
				case 3:
					input = toUpperCase(input);
					cout << endl;
					break;
				case 4:
					break;
				case 5:
					input = countConsonants(input);
					cout << endl;
					break;
				case 6:
					cout << "Please enter a word, a sentence or a string of numbers: ";
					getline(cin, input);
					break;
				case 7:
					cout << input;
					cout << endl;
					break;
				default:
					cout << "Invalid selection, please choose again or hit 0 to quit." << endl;
				break;
			}
	} while (selection != 0);
	system("pause"); 
	return 0;
}


//Function Body

int countConsonants(string input)
{
	
	char ch;
	int num = 0;
	for (int x = 1; x < input.length(); x++)
	{
		ch = tolower(input[x]);
		if (isalpha(ch) && (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u'))
			num = num + 1;
	}
	
	return num;
	
	cout << "The String has " << num << " Consonants." << endl;
}


//Header file

#include<iostream>
#include<string>
#include<iomanip>
#include<cctype>



using namespace std;

string inverseString(string);
string toUpperCase(string);
string reverseString(string);
int countConsonants(string);
Last edited on
closed account (48T7M4Gy)
Please re-edit your 3 separate files into ONE file. There is no need to separate them and it makes it easier to get you a quick response.
My apologies. Still new to the site.
closed account (48T7M4Gy)
In fact show us all your code. If that doesn't suit then what you should do is make up a new test program that just tests the function you are worried about.

For future reference that is how you should be carrying out your programming by designing,implementing and testing small sections rather than great slabs of code.

At some stage your program started to bomb and that's where you stop and concentrate rather than coding on relentlessly. It makes it easier for you especially and everybody can narrow down when you get help.

If you still want comment here then by all means give us all the code including headers. We are happy to help but you have to be part of the solution!
Second quick logic error :
1
2
3
return num;
	
	cout << "The String has " << num << " Consonants." << endl;


Should be :
1
2
	cout << "The String has " << num << " Consonants." << endl;
return num;
Does that help you? :)
Thank you for the suggestions. I recently adopted this method of programming and am focusing on this way for future programs I write. However, I did just that with this program as well after it was all coded, by commenting out the rest of my main function and testing just the function itself. It too was producing the same output.

No compile error was given, but just an output of a ? within a box. This is why I believe it has something to do with the return statement.

I have edited my last post and it contains all of my code in one box, but I shall add it in this post again to avoid any unnecessary scrolling.
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
int main()
{
	string input, result;
	int selection;
	cout << "Please enter a word, a sentence or a string of numbers: " << endl;
	getline(cin, input);
	do
	{
			cout << endl;
			cout << "USE THIS MENU TO MANIPULATE YOUR STRING " << endl;
			cout << "----------------------------------------" << endl;
			cout << "1) Inverse String" << endl;
			cout << "2) Reverse String" << endl;
			cout << "3) To Uppercase" << endl;
			cout << "4) Count Number Words" << endl;
			cout << "5) Count Consonants" << endl; // Up to here require functions
			cout << "6) Enter a Different String" << endl;
			cout << "7) Print the String" << endl;
			cout << "0) Quit" << endl;
			cin >> selection;
			cout << endl;
		
			switch (selection)
			{
				case 1:
					 input = inverseString(input);
					cout << endl;
					break;
				case 2:
					input = reverseString(input);
					cout << endl;
					break;
				case 3:
					input = toUpperCase(input);
					cout << endl;
					break;
				case 4:
					break;
				case 5:
					input = countConsonants(input);
					cout << endl;
					break;
				case 6:
				cout << "Please enter a word, a sentence or a string of numbers: ";
					getline(cin, input);
					break;
				case 7:
					cout << input;
					cout << endl;
					break;
				default:
					cout << "Invalid selection, please choose again or hit 0 to quit." << endl;
				break;
			}
	} while (selection != 0);
	system("pause"); 
	return 0;
}


//Function Body

int countConsonants(string input)
{
	
	char ch;
	int num = 0;
	for (int x = 1; x < input.length(); x++)
	{
		ch = tolower(input[x]);
		if (isalpha(ch) && (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u'))
			num = num + 1;
	}
	
	return num;
	
	cout << "The String has " << num << " Consonants." << endl;
}


//Header file

#include<iostream>
#include<string>
#include<iomanip>
#include<cctype>



using namespace std;

string inverseString(string);
string toUpperCase(string);
string reverseString(string);
int countConsonants(string);
So I am not too sure what was the problem with my original function, but it seems that once I changed my int function to a void function and instead of having a return statement, I used a cout statement; the function worked correctly.

Thank you all for your help on this. I truly appreciate you taking your time to guide me in the right direction.
Good to hear :)
closed account (48T7M4Gy)
Well done. Commenting out is a very good way to test. Saves a lot of trouble in not tracking bugs down.
Topic archived. No new replies allowed.