0

So, I think function 'round' in one case does rounding to the Grade 8, instead of Grade 7. But, if I remove it, another text getting wrong grade. in both cases the grade of either one text is wrong. What should I fix?

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

int main(void)
{
    string text = get_string("Text: ");
    int let = 0;
    for (int i = 1, n = strlen(text); i < n; i++)
    {
        if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
        {
            let++;
        }
    }
    int words = 0;
    for (int j = 1, n = strlen(text); j < n; j++)
    {
        if (text[j] == ' ')
        {
            words++;
        }
    }
    int sent = 0;
    for (int k = 1, n = strlen(text); k < n; k++)
    {
        if (text[k] == '.' || text[k] == '!' || text[k] == '?')
        {
            sent++;
        }
    }
    float L = (let * 100) / words;
    float S = (sent * 100) / words;
    int index = round(0.0588 * L - 0.296 * S - 15.8);
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if(index > 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", index);
    }
}

Here is my program

[code][1]

0

Are you sure that your letters, words and sentences counts are correct? If they're not, then the calculatiions will be broken. Seems like some of them might be off by 1.

Since you posted a screenshot instead of cutting and pasting your code, I can't test it.

If this answers your question, please click on the check mark to accept. Let's keep up on fourm maintenance. ;-)

6
  • Here is my code. Changed to 0 all counts and still the same mistake
    – slugovoy
    Jul 13 '20 at 23:53
  • where? Don't see any revised code. Try editing the question and paste in the actual code.Please don't paste code into a comment. It is just as big a nightmare as trying to transcribe from an image.
    – Cliff B
    Jul 13 '20 at 23:55
  • did it, but it came out like a plain text. Do I need to paste a code in some special way?
    – slugovoy
    Jul 14 '20 at 0:07
  • letters, words and sentences are not being counted correctly. If those are wrong, the rest doesn't matter. To help you figure it out, try printing out the total number of letters, words and sentences to see what the program says and compare it to what you count.
    – Cliff B
    Jul 14 '20 at 0:16
  • did change every int to 0, except words( set it to 1). Now program counts correctly, but grade still wrong in one text.
    – slugovoy
    Jul 14 '20 at 0:36
0

Why have you put 1 to your letters, words and sentence count?

Because it will by default result in extra +1 calculation.

Here is a preview

int letters = 0;
int words = 0;
int sentences = 0;

Get back to me if you don't understand. Please make sure to tick the answer if I helped you :)

1
  • I did like you said. Still the same mistake.do you want real code and not a screenshot to look through it?
    – slugovoy
    Jul 13 '20 at 23:52

You must log in to answer this question.

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