Cannot find the mistake 😤 Please help

Hi Guys,

I am stuck with an issue where not all the results are correct. I don’t know why does it not calculate Grade 7 and 8 correctly.

This is the output:

:slight_smile: readability.c exists
:slight_smile: readability.c compiles
:frowning: handles single sentence with multiple words
** expected “Grade 7\n”, not “Grade 8\n”**
:slight_smile: handles punctuation within a single sentence
:frowning: handles more complex single sentence
** expected “Grade 8\n”, not “Grade 9\n”**
:slight_smile: handles multiple sentences
:slight_smile: handles multiple more complex sentences
:slight_smile: handles longer passages
:slight_smile: handles questions in passage
:slight_smile: handles reading level before Grade 1
:slight_smile: handles reading level at Grade 16+

text used to test is:
7: In my younger and more vulnerable years my father gave me some advice that I’ve been turning over in my mind ever since.

8: Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, “and what is the use of a book,” thought Alice “without pictures or conversation?”

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


    int letters;
    int words;
    int sentences;

int main(void)
{



    // User prompt for input

    string text= get_string("Text: ");

    // Get text length

    int n = strlen(text);

    // add +1 if the text starts with alphanumeric letters

    if (isalnum(text[0]))
    {
        words = 1;
    }

    // count words

    for (int i = 0; i < n;  i++)
    {
        // count letters

        if (isalnum(text[i]))
        {
            letters++;
        }

        // count words

        if (i < n - 1 && isspace(text[i]) && isalnum(text[i + 1]))
        {
            words++;
        }

        // count sentences

        if (i > 0 && (text[i] == '!' || text[i] == '?' || text[i] == '.') && isalnum(text[i - 1]))
        {
            sentences++;
        }

    }

    // calculate Coleman-Liau index

    int grade = round(0.0588 * (100 * letters / words) - 0.296 * (100 * sentences / words) - 15.8);

    // debugger

    //printf("letters: %i\n wordss: %i\n sentences: %i\n", letters, words, sentences);

    // print result

    if (grade <= 1)
    {
        printf("Before Grade 1\n");
    }
    else if (grade < 16)
    {
        printf("Grade %i\n", grade);
    }
    else
    {
        printf("Grade 16+\n");
    }
}

Without the specs it’s hard to tell. Are the values of letters, words and sentences what you would expect them to be if you were calculating the values by hand? Is the grade formula correct? Off the top of my head, I’d check whether you are supposed to use round or floor.

2 Likes

Putting up the C light over City Hall for @JeremyLT.

I agree with @sfiquet. I’d double check that the counts are right and that your formula is rounding in the way you expect.

Thanks for the resppnses guys. It was a stupid mistake of not adding ‘"’ and ‘:’ so it wasn’t actually counting sentences with those at the end. All fixed now :blush:

Hi, I am doing the readability also, and seems I have the same issue like you had, may I know how your finale work? I have the same problem but I dont know how to add ""and ‘:’ in the boolean expression, thank you so much

I believe that comment refers to the line below the comment // count sentences.

Ah! My problems stuck at counting words I feel, and I would like code in ’ " ’ and ‘: ‘and ‘’’, but it does not work
else if (text[i - 1] == ’ ’ || text[i - 1] == ‘"’ || text[i - 1] ==’:’ || text[i - 1] ==’’’ )

I’d need to see your entire code to help more.

If only you knew how your problem solved mind? Thank you for sharing!!!