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;
}
1
  • It's best if you use isalpha instead if islower and isupper in function count_letters.
    – Mestica
    Commented Jun 30 at 2:56

2 Answers 2

0

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

0

You are counting the words incorrectly that's why you are having the wrong calculations for grade.

Try using a flag like inWords = true if you are inside a word then increment the value of words (by the words must be initialize to 0 since you are not counting anything yet). inWords = false when you are detecting a space or a punctuation except for punctuation marks like the hyphen and the comma, of course.

You must log in to answer this question.

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