This is my 1st post here. I am taking CS50 and I am stuck trying to figure out why the results for this weeks problem set are delivering inconsistent results.

My code is below. It passes most of the tests but on 3 tests, it reports back a grade level one below where it should. I am almost there but cannot figure out where it is reporting inconsistently.

The tests it fails on are here: https://submit.cs50.io/check50/8fdd8542fdfb2e9da21e41aecc3d31f370f18b34

Some help pointing me in the right direction would be appreciated.

int main(void)
    string s = get_string("Text: ");
    printf("Output: ");

    int letter_count = 0;
    for (int i = 0, n = strlen(s); i < n; i++)
        if(isalpha(s[i]) !=0)

    int word_count = 1;
    for (int i = 0, n = strlen(s); i < n; i++)
        if((s[i]) == ' ')

    int sentence_count = 0;
    for (int i = 0, n = strlen(s); i < n; i++)
        if(s[i] == '.' || s[i] == '!' || s[i] == '?')

    int L = letter_count * 100 / word_count;
    int S = sentence_count * 100 / word_count;

    int grade = 0.0588 * L - 0.296 * S - 15.8;

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

First, L and S should be floating point variables, using type float or double (either would work here).

Then, the computation should be using floating point division. Integer division truncates the result to be integer again (as does assigning to an integer variable), change for example 100 to 100.0 to make it a floating point division.

Also, the value assigned to grade should be rounded. Could be achieved by calling round from math.h, or, since all the relevant values are positive, by adding .5 before assigning to an integer.

  • Thank you, I got it working yesterday after I got all but one test succeeding, then I set up some print statements to figure out what was going on. Appreciate your comment, I just need to set up some functions to make the code look beautiful and we're good :) Feb 13 '20 at 8:37

You must log in to answer this question.

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