Having trouble counting characters in array

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
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
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.
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
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.
Got it, thanks guys!!
Last edited on
Topic archived. No new replies allowed.