0

I have a probelm with the readability problem set. When I run check50, I always get an error on the one sentence grade 7 check. ":( handles single sentence with multiple words expected "Grade 7\n", not "Grade 8\n"

Everything else checks out.

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

int main(void)
{
    string text = get_string("Type your text: ");
    int i;
    int num_letters = 0;
    int num_words = 1;
    int num_sentences = 0;

// iterates through the text and compares to the defined functions in ctype
    for (i = 0; i < strlen(text); i++ )
    {

        if (isalpha(text[i]))
        {
            num_letters++;
        }

        else if (isspace(text[i]))
        {
            num_words++;
        }

        else if(text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            num_sentences++;
        }

    }
// calculates reading level
    float L = (num_letters*100 / num_words);
    float S = (num_sentences*100 / num_words);
    float grade = (0.0588 * L - 0.296 * S) - 15.8;
    float total = round(grade);
        {
            if (total >= 16 )
            {
                printf ("Grade 16+\n");
            }

            else if (total < 1)
            {
                printf ("Before Grade 1\n");
            }

            else if (total >= 1 || total < 16)
            {
                printf ("Grade %i\n", total);
            }
        }
}
0

I've tested your code and i figured out where is your problem. I've suffered the same when doing this pset but i'm no expert so maybe my explanation can be simple.

The problem is where you calculate L, S and total.

I think the total value should be an int, so you can try to change:

float total = round(grade); 

to

int total = round(grade)

Then in L and S lines:

float L = (num_letters*100 / num_words);
float S = (num_sentences*100 / num_words);

In the way you wrote it although L,S are declared as float, if all the numbers are int (num_letters, num_words, num_sentences, 100) C will discard the float part and only use integers for calculation(sorry if i can't get myself explained better). I run your code printing the values of L and S with your code and get this output.

Type your text: In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
L is 417.00
S is 4.00
Grade 8

Then i replaced the 100 for 100.00, in that way C will use the decimals to calculate L, S. My changed code look like this:

float L = (num_letters*100.00 / num_words);
float S = (num_sentences*100.00 / num_words);

And for the same text before, i'm getting this result:

Type your text: In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
L is 417.39
S is 4.35
Grade 7

Hope this can help. EDIT: You can see my advice better explained in this link (under the "hints" section) https://cs50.harvard.edu/x/2020/psets/2/readability/#putting-it-all-together

You must log in to answer this question.

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