C++ recursion vowel counter

stuck. not sure how to pass the char and int. I would love to keep it global since the program functions properly, BUT my professor highly frowns on global variables..frowning as in "it's wrong = 0;" Any ideas or help to pass the str and counter to get them out of global state?
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


#include <iostream>
#include <string>
using namespace std;
	 
	int vowCount (int x);
	char str[101];
	int counter=0;
	 
int  main (){
	int vowel,x;
	 
	cout << "Enter a sentence and I'll count the  vowels: \n";
	cin.getline(str,101);
	 
	x = strlen (str); 
	vowel = vowCount(x);
	 
	cout <<  "\nThere were " <<  vowel <<  " vowels in that sentence.\n" << endl;
	
system("pause");
return 0;
} 
	int vowCount(int x){
		if (x == 0)
	    return counter;
		if(str[x-1] == 'a' 
			|| str[x-1] == 'e'
			|| str[x-1] == 'i' 
			|| str[x-1] == 'o' 
			|| str[x-1] == 'u' 
			|| str[x-1] == 'A' 
			|| str[x-1] == 'E' 
			|| str[x-1] == 'I' 
			|| str[x-1] == 'O' 
			|| str[x-1] == 'U'){
		
			counter++;
	    }
		counter = vowCount(x-1);
	    return counter;
	}
	  
	
1. Pass a pointer to a char to your vowel-count function.
2. Keep a static variable in the vowel-count function to keep track of the count.

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
51
52
53
54
#include <iostream>

int CountVowels(char* szString, int X)
{
  static int nCount = 0;

  if (X == 0)
  {
    int nTemp = nCount;
    nCount = 0;
    return nTemp;
  }

  switch (szString[X - 1])
  {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
      nCount++;
  }

  nCount = CountVowels(szString, X - 1);
  return nCount;
}

int main()
{
  const int MAXSTRING = 100;

  char  szBuffer[MAXSTRING];
  int   nCount;

  std::cout << "Enter a sentence & I'll count the vowels:" << std::endl;
  std::cin.getline(szBuffer, MAXSTRING);

  nCount = CountVowels(szBuffer, strlen(szBuffer));
  std::cout << std::endl << "There were " << nCount << " vowels in that sentence." << std::endl;
  std::cout << std::endl;

#if 0
  // system("pause") is bad
  system("pause");
#endif
  std::cin.sync();
  std::cin.peek();
  std::cin.sync();
}


Furthermore, your professor is right. Polluting the global scope is a programming evil.
http://bytes.com/topic/c/insights/737451-case-against-global-variables
Last edited on
Thank you very much! I have tried and tried with this one. Char/string recursion is quite difficult to me right now. And almost zero reference from class or book. I only use system("pause") while I'm trouble shooting code to pause it to see what the output is before it flashes away. I have never seen
[code]
#if 0
// system("pause") is bad
system("pause");
#endif

[/code

but I really like that. - Now that the coffee is wearing off and the 15 hrs of homework is kicking in, I need to review the nice code you have cleaned up.. and possible see how you are passing the input to the recursion function. (so I can cout the result of vowels.) it's either really obvious and I have a bad case of sleep deprivation, or I'm just not seeing it at all.
after looking up cin.peek() I'm guessing that is like the passing data? never used or noticed nor studied cin.peek() or cin.sync().. very interesting.
Last edited on
"Now that the coffee is wearing off and the 15 hrs of homework is kicking in, I need to review the nice code you have cleaned up.. and possible see how you are passing the input to the recursion function. (so I can cout the result of vowels.) it's either really obvious and I have a bad case of sleep deprivation, or I'm just not seeing it at all. "

WOW...complete disregard that. I am pasted tired. Again Kiana thanks SO much..you helped GREATLY !
Topic archived. No new replies allowed.