Help, function is skipping?

So I'm suppose to write a program that counts the number of upper case, lower case, words, and vowels in a sentence. And for the vowel count, I have to write it as a function. However I'm getting the problem where it doesn't display anything.
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
#include <iostream>
#include <cctype>
using namespace std;

void VowelCount (char sentence);

int main (){
    //Defined Variable counters
    int upper_count=0, lower_count=0, word_count=0;
    char letters;

    //Ask user to enter a sentence
    cout << "I can count the number of upper case, lower case,"
         << " vowels, and words." << endl;
    cout << "Please enter a sentence: ";
    cin.get(letters);

    //While loop to count the amount of upper case letters,
    //lower case, and words in the sentence;
    while (letters!='\n'){
        if((isupper(letters))){
            upper_count++;
        }
        if((islower(letters))){
            lower_count++;
        }
        if((isspace(letters))){
            word_count++;
        }
        cin.get(letters);
    }
    cout << endl;
    cout << "Number of upper case letters: " << upper_count << endl
         << "Number of lower case letters: " << lower_count << endl
         << "Number of words: " << word_count+1 << endl;
    cout << "Number of vowels: "; VowelCount(letters);
    return 0;
}
//Function to count number of vowels
void VowelCount (char sentence){
    int vowel_count=0;
    while (sentence!='\n'){
        if ((sentence=tolower(sentence))){
            if (sentence=='a' || sentence =='e' || sentence =='i' || sentence =='o' || sentence =='u'){
            vowel_count++;
            }
    }
        cout << vowel_count;
}
}


This is what I'm getting in return
1
2
3
4
5
6
7
8
I can count the number of upper case, lower case, vowels, and words.
Please enter a sentence: AEioU

Number of upper case letters: 3
Number of lower case letters: 2
Number of words: 1
Number of vowels: 
Press <RETURN> to close this window...
Last edited on
closed account (D80DSL3A)
Good thing you're not calling VowelCount() in the while loop within main(). It would run forever.
Fortunately letters does = '\n' when you do call VowelCount() near the end.

You've written VowelCount() almost as though a sequence of characters would be processed, but you're passing only a single character.

You could simplify VowelCount to just examine the one character passed to it, and return 1 if it's a vowel or 0 if it isn't.
1
2
3
4
5
6
int isVowel(char c)
{
    c=tolower(c);
    if (c=='a' || c =='e' || c =='i' || c =='o' || c =='u') return 1;
    return 0;
}

Add a vowel_count=0 variable on line 9.
Count vowels with this after line 29:
vowel_count += isVowel(letters);
Then change (existing) line 36 to:
cout << "Number of vowels: " << vowel_count << endl;
I agree with fun2code. Glad I reloaded this page before responding!

What is happening is that when you are asking for a sentence for a data member as a single character, it does not behave exactly the same way as an array. Whenever you have your get statement, it moves to the next character in the fake array, but you cannot go back (as far as I know) after that. In other words, the value of "letters" at the end, when you are attempteing to process it for vowels, is '\n', the exit condition of both your processing loop for the other factors of the sentence as well as for vowels.

Going just a hair further, cin.get(letters) in the beginning allows you to indeed put anything you want into the input stream, just saving the very first character as the actual value for letters. All the calls to cin.get after that accepts the next character from the input stream. As far as I know, the previous character is just kind of forgotten after that. If you were using an array instead, it would be as easy as processing from the base in the vowel count call.
Last edited on
Topic archived. No new replies allowed.