0

I've written all of my code and tested that the letter, sentence and word output is counting correctly, which it seems to be. I even tested with all of the sentences on the instructions page and that seems to work fine. As I wrote each block, I used printf to see my output and that all seems to be accurate, but when I run check50, only the first two tests pass: "readability.c exists" and "readability.c compiles". I feel like my int final_grade is rounding off incorrectly.

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

int get_letters(string text);
int get_words(string text);
int get_sentences(string text);


//main function
int main()
{
    string text = get_string("Text: ");
    //pass arguments to helper functions
    float letters = get_letters(text);
    float words = get_words(text);
    float sentences = get_sentences(text);

    // float f_letters = letters;
    // float f_sentences = sentences;

    // Calculate the letters and sentences per 100 words of the text with floats once helpers are done 100 * the variable / words
    float L = 100.0 * letters / words;
    float S = 100.0 * sentences / words;
    // printf("L is %f and S is %f\n", L, S);

    //Coleman-Liau index
    float grade = 0.0588 * L - 0.296 * S - 15.8;
    int final_grade = round(grade);

    // printf("%i\n", grade);
    if (final_grade < 1)
    {
        //Don't add the variable when calling Before Grade 1, otherwise, the number may be well below 1
        return printf("Before Grade 1\n");
    }
    else if (final_grade >= 16)
    {
        //Don't add the variable when calling 16+, otherwise, you will return the exact number which in this case, you don't want to return
        return printf("Grade 16+\n");
    }
    else
    {
        return printf("Grade %i\n", final_grade);
    }
}

//add helper functions
// count letters
int get_letters(string text)
{
    int count = 0;

    for (int i = 0; i <= strlen(text); i++)
    {
        if (text[i] >= 'A' && text[i] <= 'z')
        {
            count++;
        }
    }
    return count;
}

// //count words
int get_words(string text)
{
    //count the first word, no preceding space
    int count = 1;

    for (int i = 0; i <= strlen(text); i++)
    {
        if (text[i] == ' ')
        {
            count++;
        }
    }
    return count;
}
// //count sentences
int get_sentences(string text)
{
    int count = 0;

    for (int i = 0; i <= strlen(text); i++)
    {
        if (text[i] == '.' || text[i] == '?' || text[i] == '!')
        {
            count++;
        }
    }
    return count;
}

check50

cs50/problems/2020/x/readability
:) readability.c exists
Log
checking that readability.c exists...
:) readability.c compiles
Log
running clang readability.c -o readability -std=c11 -ggdb -lm -lcs50...
:( handles single sentence with multiple words
expected "Grade 7\n", not "Grade 8\n"
Log
running ./readability...
sending input In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since....
checking for output "Grade 7\n"...

Expected Output:
Grade 7
Actual Output:
Grade 8
:( handles punctuation within a single sentence
expected exit code 0, not 8
Log
running ./readability...
sending input There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy....
checking for output "Grade 9\n"...
checking for EOF...
checking that program exited with status 0...
:( handles more complex single sentence
expected exit code 0, not 8
Log
running ./readability...
sending input 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?"...
checking for output "Grade 8\n"...
checking for EOF...
checking that program exited with status 0...
:( handles multiple sentences
expected exit code 0, not 8
Log
running ./readability...
sending input Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard....
checking for output "Grade 5\n"...
checking for EOF...
checking that program exited with status 0...
:( handles multiple more complex sentences
expected exit code 0, not 9
Log
running ./readability...
sending input It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him....
checking for output "Grade 10\n"...
checking for EOF...
checking that program exited with status 0...
:( handles longer passages
expected exit code 0, not 8
Log
running ./readability...
sending input When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow. When it healed, and Jem's fears of never being able to play football were assuaged, he was seldom self-conscious about his injury. His left arm was somewhat shorter than his right; when he stood or walked, the back of his hand was at right angles to his body, his thumb parallel to his thigh....
checking for output "Grade 8\n"...
checking for EOF...
checking that program exited with status 0...
:( handles questions in passage
expected exit code 0, not 8
Log
running ./readability...
sending input Would you like them here or there? I would not like them here or there. I would not like them anywhere....
checking for output "Grade 2\n"...
checking for EOF...
checking that program exited with status 0...
:( handles reading level before Grade 1
expected exit code 0, not 15
Log
running ./readability...
sending input One fish. Two fish. Red fish. Blue fish....
checking for output "Before Grade 1\n"...
checking for EOF...
checking that program exited with status 0...
:( handles reading level at Grade 16+
expected exit code 0, not 10
Log
running ./readability...
sending input A large class of computational problems involve the determination of properties of graphs, digraphs, integers, arrays of integers, finite families of finite sets, boolean formulas and elements of other countable domains....
checking for output "Grade 16+\n"...
checking for EOF...
checking that program exited with status 0...
0
0

The problem here is your return printf(..., which should be just printf(..., no return. main is the only function that declares to return something, but does not need to return a value explicitly. In that case, the compiler would insert the equivalent of return 0; at the end. You could still have an explicit return 0; at the end of the function.

The value you accidentally return is the number of printed characters.

Also,

if (text[i] >= 'A' && text[i] <= 'z')

Poor characters [\]^_` being mistaken for letters.

You must log in to answer this question.

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