Need help counting vowels.

My program is supposed to count the number of vowels in a file using recursion. When I run this program I always get the number of vowels is 0. I'm pretty sure that is because it outputs the value for length instead the value for vowelcount but I don't understand why length is 0 anyway. Please help.

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

ifstream infile;
int vowels(char letters[], int vowelcount, int length);
void reading(char letters[], int length);


int main()
{
    
    char letters[6];
   int vowelcount=0, length=0;
    
    reading(letters, length);
    
   
    
   cout<<"there are "<<vowels(letters, vowelcount, length)<<" vowels"<<endl;;
    
    system ("PAUSE");
return 0;
}
void reading(char letters[], int length)
{
     infile.open("lab18letters.txt");
     
     for(int i=0; i<6; i++)
     {
       infile>>letters[i];
       cout<<letters[i]<<endl;
       length++;
     }
}
int vowels(char letters[], int vowelcount, int length)
{
    if (length == 0)
       return 0;
    else if(length>0)
    {
     if(letters[1]=='a'||letters[1]=='e'||letters[1]=='i'||letters[1]=='o'||letters[1]=='u')
    {
    vowelcount++;
    }
    return vowels(letters+1, vowelcount, length-1);
    }
}
two things you can try mate dont know if it will help as im a noob to but i think you program is returning 0 so remove the return 0;
and on the last line try

return vowels(letters = letters+1, vowelcount, length-1);
hope i help
@markkirby---- are you talking about the main function?
Last edited on
yes I had a broblem a bit like that I remove the return 0 and it worked i think it was because of having 2 return functions
not to sure but worth a try
@OP:
Look up "passing by reference" vs. "passing by copy"...
Also, you can make this program easier by working with strings, instead of char[]s.

@markkirby:
return 0 in main is fine. It explicitly says (to your OS) that your program ran correctly. And I would look up how recursion works because I don't think that line of code does what you want it to do...!
Last edited on
If I remove the return 0 I don't know what to replace it with. I tried replacing it with return vowelcount but that is still 0 too. I also changed the last line toreturn vowels(letters = letters+1, vowelcount, length-1); ; but nothing changed. I hate recursion.
sorry i could not help
@mathhead thanks for explaing the return 0 i was just told to put it on the end of everything
@Emperorlu---- is your teacher asking for you to use recursion ?
@mathhead
Originally my program was using strings and I decided to switch to an array because I had no idea how to go about checking if each letter was a vowel. The only way that I know how to check each letter in a line of characters is through an array. I'll try it again and see if I can get anything to work.

@kapo
Yes he is. I could do this with no problem using loops.
Last edited on
k, ima try to figure it out.
Thanks for all of the help. I finally finished the program.
Topic archived. No new replies allowed.