Vowel/Consonant counting function using cin.get();

Hi.
I am attempting to write a program that counts vowels, consonants, and white spaces. i have to use cin.get(); and a function.
This is what i have.

#include <iostream>
using namespace std;

int main(){
int vowels=0, consonants=0, spaces=0, vowelCounter(char, int), consonantCounter(char, int), spaceCounter(char, int);
char ch;
cout<<"Enter lines of characters terminated by !.\n";
cin.get(ch);
vowels = vowelCounter (ch, vowels);
consonants = consonantCounter (ch, consonants);
//spaces = spaceCounter (ch, spaces);
cout<<"There are "<<vowels<<" vowels in the text."<<endl;
cout<<"There are "<<consonants<<" consonants in the text."<<endl;
cout<<"There are "<<spaces<<" white spaces in the text."<<endl;
}

int vowelCounter (char ch, int vowels){
while (ch != '!'){
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
vowels++;}
cin.get(ch);
}
return (vowels);
}
int consonantCounter (char ch, int consonants){
while (ch != '!'){
if (ch!='a'||ch!='e'||ch!='i'||ch!='o'||ch!='u'||ch!='A'||ch!='E'||ch!='I'||ch!='O'||ch!='U'||ch!=' '){
consonants++;}
cin.get(ch);
}
return (consonants);
}

i purposfully dont have a function written for the spaces which is why its commented in the main. both functions work on their own, but dont work when both being called. help?
Last edited on
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
#include <iostream>
using namespace std;

int main(){
int vowels=0, consonants=0, spaces=0, vowelCounter(char, int), consonantCounter(char, int), spaceCounter(char, int);
char ch;
cout<<"Enter lines of characters terminated by !.\n";
cin.get(ch);
vowels = vowelCounter (ch, vowels);
consonants = consonantCounter (ch, consonants);
//spaces = spaceCounter (ch, spaces);
cout<<"There are "<<vowels<<" vowels in the text."<<endl;
cout<<"There are "<<consonants<<" consonants in the text."<<endl;
cout<<"There are "<<spaces<<" white spaces in the text."<<endl;
}

int vowelCounter (char ch, int vowels){
while (ch != '!'){
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
vowels++;}
cin.get(ch);
}
return (vowels);
}
int consonantCounter (char ch, int consonants){
while (ch != '!'){
if (ch!='a'||ch!='e'||ch!='i'||ch!='o'||ch!='u'||ch!='A'||ch!='E'||ch!='I'||ch!='O'||ch!='U'||ch!=' '){
consonants++;}
cin.get(ch);
}
return (consonants);
}
It looks like you are ignoring the function hierarchy

So, in other languages (such as Java or Visual Basic), function order DOES NOT MATTER.

In C++, function order DOES MATTER.

Think of it like a pyramid. The function that calls the most other functions is the one that needs to be at the bottom. Since main is the one calling all the functions, you should copy and paste it in the bottom. So here,

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
#include <iostream>
using namespace std;

int vowelCounter (char ch, int vowels){
while (ch != '!'){
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){
vowels++;}
cin.get(ch);
}
return (vowels);
}
int consonantCounter (char ch, int consonants){
while (ch != '!'){
if (ch!='a'||ch!='e'||ch!='i'||ch!='o'||ch!='u'||ch!='A'||ch!='E'||ch!='I'||ch!='O'||ch!='U'||ch!=' '){
consonants++;}
cin.get(ch);
}
return (consonants);
}


int main(){
int vowels=0, consonants=0, spaces=0, vowelCounter(char, int), consonantCounter(char, int), spaceCounter(char, int);
char ch;
cout<<"Enter lines of characters terminated by !.\n";
cin.get(ch);
vowels = vowelCounter (ch, vowels);
consonants = consonantCounter (ch, consonants);
//spaces = spaceCounter (ch, spaces);
cout<<"There are "<<vowels<<" vowels in the text."<<endl;
cout<<"There are "<<consonants<<" consonants in the text."<<endl;
cout<<"There are "<<spaces<<" white spaces in the text."<<endl;
}


The only difference is that I placed main in the bottom. See if that works :)
Last edited on
Thanks! I'll try that the next time I'm in front of a unix machine and let you know if it worked
I tried that but i still didnt get the outcome i wanted. after i type in "i love fruit!" it should tell me that there are 5 vowels and 5 consonants but it makes me type another ! and then says i have 5 vowels and 2 consonants
Topic archived. No new replies allowed.