Can not find any mistake(CS50 online course)

Hi guys, I can not find bugs in my code :dizzy_face: please help
Thank you in advance!!!

This programes is to computes the approximate grade level needed to comprehend some text, per the below.

$ ./readability
Text: Congratulations! Today is your day. You’re off to Great Places! You’re off and away!
Grade 3

And I stuck at the following two test text:
first text I should get Grade 7 but output is Grade 8,
second text should be Grade 8 but output is Grade 9.

The text used to test is:
Grade 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.

Grade 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 <string.h>
#include <ctype.h>
#include <math.h>


int countLetters(string text);
int countWords(string text);
int countSentences(string text);

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

    int numberOfLetters = countLetters(s);

    int numberOfWords = countWords(s);

    int numberOfSentences = countSentences(s);


    float L = 100 * numberOfLetters / numberOfWords;
    float S = 100 * numberOfSentences / numberOfWords;

    int index = round(0.0588 * L - 0.296 * S - 15.8);

    if (index < 1)
    {
        printf("Before Grade 1");
    }
    else if (index >= 16)
    {
        printf("Grade 16+");
    }
    else
    {
        printf("Grade %i", index);
    }

}

int countLetters(string text)
{
    int numberOfLetters = 0 ;

    for (int i = 0 ; text[i] != '\0' ; i++)
    {
        bool isCurrentCharAnAlpha = isalpha(text[i]);
        if (isCurrentCharAnAlpha)
        {
            numberOfLetters++;
        }
    }
    return numberOfLetters;
}

int countWords(string text)
{
    int numberOfWords = 0 ;
    for (int i = 0 ; text[i] != '\0' ; i++)
    {
        bool isCurrentCharAnAlpha = isalpha(text[i]);
        if (isCurrentCharAnAlpha)
        {
            if (i == 0)
            {
                numberOfWords++;
            }
            else if (text[i - 1] == ' ')
            {
                numberOfWords++;
            }
        }
    }
    return numberOfWords;
}

int countSentences(string text)
{
    int numberOfSentences = 0 ;
    for (int i = 0 ; text[i] != '\0' ; i++)
    {
        if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            numberOfSentences++;
        }
    }
    return numberOfSentences;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Thank you so much! :grinning:

Alrighty, lets start simple. How does your result differ from what you expect?

1 Like

There are two tests text, first text’s result should be Grade 7, but it is Grade 8.
the second text should be Grade 8 but the result is Grade 9.