invalid types `char[int]'

Hi. I'm trying to have this program count the number of letters input by the user as response to line 18's request,"\nEnter the player's batting record: ". The problem is that I'm getting this error:

invalid types `char[int]' for array subscript


The error is in response to lines 23-26 where I have size[b]. Here's the 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
#include <iostream>
#include <stdio.h>
using namespace std;

int main ()
{
    int player;                 //Assign player number
    double H;                   //Assign variable for hits
    double O;                   //Assign variable for outs
    double sum;                 //Assign variable for sum of only H and O
    double average;             //Assign variable for average of H and O
    char size;                  //Allows compiler to view user input as array.
    int b;                      //Assign variable for integer size
    int letters = 0;            //Assing value of 0 to allow compiler to count.
    
    cout << "\t\t\tBatting Average Calculator\t\t";
    
    cout << "\n\nEnter the player's number: ";
    cin >> player;
   
    cout << "\nEnter the player's batting record: ";
    cin >> size;
       { b = 0;
         while (size[b] !=0)
         if (( size[b] >= 'a' && size[b] <= 'z') || (size[b] <= 'A' && size[b] >= 'Z'))
         { letters++; b++; }}
}


Any help/input would be greatly appreciated! Thank you :)
size if a single char. Did you intend to make it some kind of string?
Yeah, I need it to be a string.....but I'm really confused and am not even sure how to do that :/. I need the compiler to read the user input as a string and count the number of letters input
I suggest you read up a bit on arrays

http://www.cplusplus.com/doc/tutorial/arrays/

You've not declared size as an array of char but rather just a char so size[b] doesn't really make sense.
To declare an array of character you do this char size[ARRAY_SIZE].
Thanks!
Easy way to do this is probably make size a string and then just do size.length()
The length function returns the length of the string, in terms of number of characters. http://www.cplusplus.com/reference/string/string/length/
Topic archived. No new replies allowed.