0

I' at the very end of readability and am having trouble getting the math correct and rounding it. I'm getting the correct letters, words and sentences, but am getting wildly wrong values for the grade level. As far as I can tell, everything in the equation is a float, so things shouldn't get lopped off.

Any help would be much appreciated! Thanks!

edit I fixed the rounding errors

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

float count_letters(string read);
float count_words(string read);
float count_sentances(string read);
float get_grade(string read);



int main(void)
{


    string read = get_string("Text: "); // ask for sentance to analyze
    float Grade = get_grade(read);
      printf(" Grade %f\n", round (Grade));


}


float count_letters(string read)
{
    int length = 0;
    for (int i = 0, n = strlen(read); i < n; i++)
    {
        if (isupper(read[i]) || islower(read[i]))
        {
            length++;
        }
    }
    return length;
}

float count_words(string read)
{
    int words = 1;
    for (int i = 0, n = strlen(read); i < n; i++)
    {
        if (read[i] == ' ')
        {
            words++;
        }
    }
    return words;
}

float count_sentances(string read)
{
    int sen = 0;
    for (int i = 0, n = strlen(read); i < n; i++)
    {
        if (read[i] == '.' || read[i] == '?' || read[i] =='!')
        {
            sen++;
        }
    }
    return sen;
}

float get_grade(string read)
{
    float letters = count_letters(read);
   // printf("letters: %f\n", letters);
    float words = count_words(read);
    //printf("words: %f\n", words);
    float sens = count_sentances(read);
    //printf("sens: %f\n", sens);

    float L = 100.0 * (letters / words);
    float S = 100.0 * (sens / words);

    float index = 0.588 * L - 0.296 * S - 15.8;

    return index;
}
0

Simple typographical error. Order of magnitude off for the calculations.

You must log in to answer this question.

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