1

The problem is that when i run check50 cs50/problems/2020/x/readability into the terminal, it says

:( handles questions in passage
Cause
expected "Grade 2\n", not "Grade 3\n"
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"...
Expected Output:
Grade 2
Actual Output:
Grade 3

and it is the only thing that is wrong, there seems to be no other errors. Is it because in the sentence there are others dots at the end ? Because in the walkthrough it said that it is normal if i get more sentences in that case, and i can't see anything else changing the grade. Here is my code if needed :

#include<stdio.h>
#include<cs50.h>
#include<ctype.h>
#include<math.h>
int main(void)
{
    float letters = 0;
    float words = 1;
    float sentences = 0;
    int isalpha;
    int isspace;
    string text = get_string("Text :"); //ask for input

    for (int i = 0; text[i] != '\0'; i++)
    {
        if (isalpha(text[i]) != 0)
        {
            letters++;
        }
        else if (isspace(text[i]) != 0)
        {
            words++;
        }
        else if (text[i] == '?' || text [i] == '!')
        {
            words--;
            sentences++;
        }
        else if (text[i] == '.')
        {
            sentences++;
        }

    }

    float avgl = (100 / words * letters); //average of letters per 100 words
    float avgs = (100 / words * sentences); //average of sentences per 100 words

    float index = ((0.0588 * avgl) - (0.296 * avgs) - 15.8); //calcul de l'index
    int total = round(index);

    if (total < 1) //check si moins de 1
    {
        printf("Before Grade 1\n");
    }
    else if (1 <= total && total <= 16) //check si entre 1 et 16
    {
        printf("Grade %i\n", total);
    }
    else if (total > 16) //check si c'est plus de 16
    {
        printf("Grade 16+\n");
    }


}
1

the reason that your program is not working is because you are decrementing words with each question mark and exclamation point. If you remove words--; from your program, it works correctly!

By the way, the actual inputs are listed here: https://cs50.harvard.edu/x/2020/psets/2/readability/#:~:text=How%20to%20Test%20Your%20Code

The test input that your program failed is actually "Would you like them here or there? I would not like them here or there. I would not like them anywhere."

If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!

1
  • 1
    ohhhh thank you very much ! When I type I am used to put question marks and exclamation point after a space bar so i corrected my code in the way that I write, I get it now thanks !
    – Ralph
    Aug 22 '20 at 15:33

You must log in to answer this question.

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