Having trouble counting characters in array

Mar 24, 2013 at 4:06am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void charactSummary(char counterArray[], char& setOfChar)
{
    counterArray[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    //this counterArray keeps showing an error message of "expected primary-expression before '{' token."
    //any ideas as to what I'm doing wrong. Very Very new at c++.
    char charSum;
    int counter;
    counterArray[0] = 'a' - 97;
    for(charSum = 'a'; charSum < 'z'; charSum++)
    {
        counter = 0;
        counterArray[charSum];
        if (setOfChar == counterArray[charSum])
        {
            counter++;
        }
    }
    for(charSum = 'a'; charSum < 'z'; charSum++)
    {
        cout << counterArray[charSum] << " = " << counter << endl;
    }
}
Last edited on Mar 25, 2013 at 12:53am
Mar 24, 2013 at 5:06am
14
15
 char genCharArray[i], counterArray[charSum], setOfChar;
    char counter [charSum];
variables i and charSum don't have a value. and I don't think you can initialize an array with a char variable anyway.
Last edited on Mar 24, 2013 at 5:19am
Mar 24, 2013 at 9:00am
I believe you can only initialize character arrays (c-strings) with the equal sign. When assigning a new string to an existing character array, you should use the strcpy function.

You should also fix the problems Yanson pointed out.
Mar 24, 2013 at 10:35am
Parameter char counterArray[] of function
void charactSummary(char counterArray[], char& setOfChar)

has type char *

That is any array declared as a parameter passed by value implicitly converted to a poinetr to its first element.
So you may not write such a statement as
counterArray[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

Last edited on Mar 24, 2013 at 10:36am
Mar 24, 2013 at 1:08pm
Thanks everyone, I wasn't expecting help this quickly. You all are great, I'm trying to fix the problems now. I'll update letting everyone know as soon as I'm done.
Mar 25, 2013 at 12:08am
Got it, thanks guys!!
Last edited on Mar 25, 2013 at 8:36pm
Topic archived. No new replies allowed.