0

So I understand I have a math error in this code. I assume it is a rounding error which I will eventually get to. In either case, many times I do return the correct grade level but the check50 function is telling me my answer is still wrong. Here is a link to the check50 output.

cs50 check output

Here is a link to the code:

disregard the commented out printf functions. I was trying to zero in on some problems.

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


float L (int a, int b); //calculates letters per words
float S (int x, int y); //calculates words per sentence


int main (void)
{
    int letters = 0;
    int words = 0;
    int sent = 0;
    float index = 0;

    string t = get_string("Enter a line of text for grading:  ");

    for(int i=0; i<strlen(t); i++)
    {
        if(t[i]!=32 && t[i]!=33 && t[i]!=46 && t[i]!=63) //discounts characters that are not letters
        {
            letters++;
        }
        if(t[i]==32|| t[i]==33 || t[i]==46 || t[i]==63) //determines how to count words by looking for punctuiation and spaces
        {
            words++;
        }
        if (t[i]==33 || t[i]==46 || t[i]==63)// looks for sentences by finding punctiuation

        {
            sent++;
        }
        if (t[i]==32 && (t[i-1]==32 || t[i-1]==33 || t[i-1]==46 || t[i-1]==63)) //corrects for two spaces after punctuation
        {
            words--;
        }
    }

        index = .0588*L(letters, words)-.296*S(words, sent)-15.8; //index formula

        if(index<1) //output loops
        {
            printf("Before Grade 1 \n");
        }
        if (index>1 && index < 16.5)
        {
            printf("Grade %.0f \n", index);
        }
        if (index>=16.5)
        {
            printf("Grade 16+ \n");
        }


        //printf ("index number:  %.3f \n", index);

       // printf ("L: %.2f \n", L(letters, words));
        //printf ("S: %.2f \n", S(words, sent));
        //printf ("letters: %i \n",letters);
        //printf ("words: %i \n", words);
        //printf ("sentences: %i \n", sent);
}

float L (int a, int b)
{
   return (a * 100)/b;
}

float S (int x, int y)
{
    return (y*100)/x;
}

Can someone help me out?

Thanks in advance.

2
  • I think there's a space after your Reading Level outputs, and in the check50 output there are no spaces. Commented Apr 9, 2020 at 4:45
  • i appreciate the help, this didn't get it done, but it was for sure something that I missed.
    – Rob Watt
    Commented Apr 10, 2020 at 3:33

1 Answer 1

0

Your code is good except that in the way you are calculating the letters, you're not filtering caracters like " , or others. You can try to add up a letter ONLY if it is an alphabetical caracter uppper or lower case.

1
  • Thanks for the tip. I will update my code and see if that fixes it!
    – Rob Watt
    Commented Apr 10, 2020 at 3:34

You must log in to answer this question.

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