letters in a string

Oct 4, 2012 at 11:54am
i am trying to make a program that will count the consonants and vowels in a given string.

Here's my code:
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
#include<iostream>
#include<string>
using namespace std;

void countVC(string input, int &vCount, int &cCount)
{
     vCount = 0;
     cCount = 0;
     for(int i = 0; i <= input[i]; i++)
     {
         if(input[i] == 'a' || input[i] == 'A')
             vCount = vCount + 1;
         else if(input[i] == 'e' || input[i] == 'E')
             vCount = vCount + 1;
         else if(input[i] == 'i' || input[i] == 'I')
             vCount = vCount + 1;
         else if(input[i] == 'o' || input[i] == 'O')
             vCount = vCount + 1;
         else if(input[i] == 'u' || input[i] == 'U')
             vCount = vCount + 1;
         else
             cCount = cCount + 1;
     }
}
int main()
{
  string inp = "the fox";
  int vc, cc,
  countVC(inp, vc, cc);
  cout << "input      : " << inp << endl;
  cout << "vowels     : " << vc << endl;
  cout << "consonants : " << cc << endl;
  
  system("pause");
  return 0;
}


and the output should be like this:
input      : the fox
vowels     : 2
consonants : 4


every time I compile it, the compiler always says:
In function `int main()':
initializer expression list treated as compound expression

can anyone help me? any help is highly appriciated
thank you and god bless!!
Last edited on Oct 4, 2012 at 11:54am
Oct 4, 2012 at 12:05pm
int vc, cc,
in this line you must put ;
so to do like this:
int vc, cc;
Oct 4, 2012 at 12:11pm
But because your program count the space as consonants you should fix something.
(i fixxed that for you.).
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
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;

void countVC(string input, int &vCount, int &cCount)
{
     vCount = 0;
     cCount = 0;
     for(int i = 0; i <= input[i]; i++)
     {
         if(input[i] == 'a' || input[i] == 'A')
             vCount = vCount + 1;
         else if(input[i] == 'e' || input[i] == 'E')
             vCount = vCount + 1;
         else if(input[i] == 'i' || input[i] == 'I')
             vCount = vCount + 1;
         else if(input[i] == 'o' || input[i] == 'O')
             vCount = vCount + 1;
         else if(input[i] == 'u' || input[i] == 'U')
             vCount = vCount + 1;
         else if(input[i]==' '){
              cCount;
              vCount;}
         else
             cCount = cCount + 1;
     }
}

int main()
{
  string inp = "the fox";
  int vc, cc;
  countVC(inp,vc,cc);
  cout << "input      : " << inp << endl;
  cout << "vowels     : " << vc << endl;
  cout << "consonants : " << cc << endl;

  system("pause");
  return 0;
}

here it is.
So now instead of print
2
5
will give the correct results
2
4

hope that helped
Oct 4, 2012 at 12:23pm
thank you very much skarla for the help! :)
Topic archived. No new replies allowed.