I need the findCons function to return the number of consonants but it keeps giving me a weird number. I know there's a lot of other stuff going on but i'm just trying to figure out this particular function first.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int findCons(char* c);
int findVows(char* c);
int main()
{
int choice;
char* c = "Hello World";
cout << "\t\t Menu \n\n"
<< "1. Count the number of vowels in the string.\n"
<< "2. Count the number of consonants in the string.\n"
<< "3. Count both the vowels and consonants in the string.\n"
<< "4. Enter another string.\n"
<< "5. Exit the program.\n";
cin >> choice;
if (choice == 1)
{
findVows(c);
}
else if (choice == 2)
{
findCons(c);
}
else if (choice == 3)
{
findCons(c);
findVows(c);
}
/*
else if (choice == 4)
{
cout << "Please enter another string." << endl;
getline(cin, c);
}
any chance your input has spaces, punctuation, or other such things? Your function will count those because they are not in the "not these" hard coded lists you provided.
also, your loop looks wrong.
for (int i = 0; i != '/0'; i++)
this is the same as
for(I = 0; I < 0; I++) //does nothing?
what you wanted was
for(I = 0; c[I] != 0; I++)
I think.
one way to make this a lot easier to deal with is to use toupper or tolower to only check 1/2 as much stuff.
one way to exclude other junk is to do something like
tolower...
if(c[I] > 'z' || c[I] < 'a') //if this is true, the letter is neither vowel nor consonant. do nothing...
...
if(c[I] == 'a' || ... =='u')
++
This is wrong: char* c = "Hello World";
A string literal is an array of const char
This is also wrong: getline(cin, c);
1. std::getline() is used in conjunction with std::string. Use std::cin.getline() instead.
2. We would need to declare an array of char and read into that.
You have #include <string>.
Strongly favour std::string over C-style null-terminated byte strings.
why it does not return 0 and print 1 is a mystery though.
you also need the fixes mentioned above, the allocation of your hello world string which I didn't notice, and the getline, which I mentioned, and read a c-string (char array) or string.
The for loop is equivalent to for (int i = 0; i != 12080; i++). So the loop executes 12080 times.
The condition at line 5 is always true. Every character doesn't equal 'a' or doesn't equal 'A'. So you always increment consonants. Since the loop exeutes 12080 times, you increment consonants to 12080.