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.
// 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;
}
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.
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!
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.
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.