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:
#include <iostream>
#include <stdio.h>
usingnamespace 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 :)
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
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].
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/