Search code examples
cexceptionpointcs50floating

C floating point exception error . . . on a formula for calculating reader grade level


this is a program that counts the number of letters, words, and sentences from input text. in the final section, I am attempting to use a formula and I keep getting a floating point exception. Please help.

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

int main(void)
{
    char text[100];
    int number;
    int words = 0, i;
    int sentences = 0, j;
    float l = number / words * 100;
    float s = sentences / words * 100;
    int index = 0.0588 * l - 0.296 * s - 15.8;
    int grade = round(index);

    // Text Input = Text //

    printf("Text: ");
    fgets(text, sizeof(text), stdin);
    printf("%s", text);

    // Letters = number //

    number = strlen(text);
    printf("%d letters\n", number);

    // Words = words //

    for (i = 0; text[i] != '\0'; i++)
    {
        if (text[i] == ' ' && text[i+1] != ' ')
        words++;
    }
    printf("%d words\n", words + 1);

    // Sentences = sentences

    for (j = 0; j < strlen(text); j++)
        {if (text[j] == '.' || text[j] == '!' || text[j] == '?')
            sentences++;
        }

        printf("%d sentences\n", sentences);

    // grade level based on formula //

    if (index >= 1 && index <= 16)
        {
            printf("Grade %d\n", grade);
        }
             else
                {
                  if (index < 1)
                    {
                printf("Before Grade 1\n");
                    }

                if (index > 16)
                    {
                printf("Grade 16+\n");
                    }
                }
        }

I keep getting a floating point exception with the final section starting at grade level based on formula . . . the float l, float s, int index, int grade are involved with the final section . . . no idea what to do


Solution

  • In modern C code, practically the only way to get a floating point exception is to do integer division by zero. Yes, I mean "integer". With floating point arithmetic, you merely get an infinity, or possibly a NaN if the dividend was already a NaN.

    You have:

    int number;
    int words = 0, i;
    int sentences = 0, j;
    float l = number / words * 100;
    float s = sentences / words * 100;
    int index = 0.0588 * l - 0.296 * s - 15.8;
    int grade = round(index);
    

    You can't calculate l or s until you've counted the number of letters (you're not doing that correctly when you try, later, but that's a separate discussion for now), and the number of words, and the number of sentences. C doesn't remember how to do the calculation later; it does it when you initialize l and s. And since words is zero, you get a floating point exception because you're dividing by zero.

    Do the computation of l and s after you've calculated number, words, sentences. And you need to do a floating operation, probably — so you need to convert words (or number and sentences) to double (or float if you insist, but use double unless there's a compelling reason not to).

    You'll need to fix the index and grade calculations too. In particular, the assignment to index truncates (not rounds) the value; the round() function is a no-op since the input is an integer.

    int number;
    int words = 0, i;
    int sentences = 0, j;
    
    …count letters, words, sentences…
    
    double l = number / (double)words * 100;
    double s = sentences / (double)words * 100;
    int grade = round(0.0588 * l - 0.296 * s - 15.8);
    

    Note that strlen(text) does not give the number of letters. It gives the number of characters (bytes, strictly) including white space (newline, blanks) and punctuation and digits.

    I'm not convinced by your word and sentence counting, either, but that's something you'll need to review on your own. It depends in part on how kind the input data is. I think your code will miscount weird sequences...but I may be wrong!! (Did that triple-dot and that double exclamation get miscounted?)