0

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?

0

letters is an int. It looks like it should be the number of letters in your text. But it looks like you're switching letters and text in your code, especially since you've got text++; in the "counting the letters" section.

What the error message is telling you is that the "subscripted value" isn't an array (or pointer or vector). The subscripted value here is letters, and it's declared as an int, which isn't any of those three.

| improve this answer | |
  • On revisiting i found a LOT of mistakes in the code including the letters and text mixup. Thanks! – starbvuks Jun 30 at 23:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .