Readability (Problem Set 2)
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
// Letters detects letters
// Words detects spaces
// Sentences detects periods, exclamation points, question marks
int main(void)
{
int n, letters = 0, words = 1, sentences = 0;
string text;
text = get_string("Text: ");
n = strlen(text);
for (int i = 0; i < n; i++)
{
//letters
if (letters[i] >= 'a' && letters[i] <= 'z') || (letters[i] >= 'A' && letters[i] <= 'Z')
{
text++;
}
//words
if (letters[i] == ' ')
{
words++;
}
//sentences
if (text[i] == '!' || text[i] == '.' || text[i] == '?')
{
sentences++;
}
}
//Coleman-Liau formula
float L = (100 * ((float) letters / (float) words));
float S = (100 * ((float) sentences / (float) words));
float grade0 = (0.0588 * L) - (0.296 * S) - 15.8;
int grade1 = (round(grade0));
if (grade1 >= 1 && grade1 < 16)
{
printf("Words: %i\n", (int) round(words));
}
else if (grade1 < 1)
{
printf("Before Grade 1");
}
else
{
printf("Grade 16+");
}
printf("\n");
}
After trying to compile the following code, this keeps happening:
readability.c:24:20: error: subscripted value is not an array, pointer, or vector
if (letters[i] >= 'a' && letters[i] <= 'z') || (letters[i] >= 'A' && letters[i] <= 'Z')
~~~~~~~^~
readability.c:24:41: error: subscripted value is not an array, pointer, or vector
if (letters[i] >= 'a' && letters[i] <= 'z') || (letters[i] >= 'A' && letters[i] <= 'Z')
~~~~~~~^~
readability.c:24:53: error: expected expression
if (letters[i] >= 'a' && letters[i] <= 'z') || (letters[i] >= 'A' && letters[i] <= 'Z')
^
readability.c:24:64: error: subscripted value is not an array, pointer, or vector
if (letters[i] >= 'a' && letters[i] <= 'z') || (letters[i] >= 'A' && letters[i] <= 'Z')
~~~~~~~^~
readability.c:24:85: error: subscripted value is not an array, pointer, or vector
if (letters[i] >= 'a' && letters[i] <= 'z') || (letters[i] >= 'A' && letters[i] <= 'Z')
~~~~~~~^~
5 errors generated.
<builtin>: recipe for target 'readability' failed
make: *** [readability] Error 1
What can i do to make C read [i] as a valid array?