-1

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
maaaaannn
  • 157
  • 1
  • 1
  • 5
  • `number` doesn't have a value assigned. – dbush Apr 15 '20 at 03:45
  • hum. Googling for "floating-point exception" returns a box that say "A floating point exception is an error that occurs when you try to do something impossible with a floating point number, such as divide by zero." This is indeed your problem (`int words = 0;` followed by `number / words * 100`). You didn't put much effort into this :( – ikegami Apr 15 '20 at 03:48

2 Answers2

1

You intialize words to 0 and do not update it before you divide, giving you an integer divide-by-zero error.

You need to fix your algorithm and calculate number, words and sentences before you do math on them. Once you’ve done that, you’ll still have at least one other bug to fix.

The expressions number / words and sentences / words are buggy, because both operands have type int. Dividing int by int gets you an int, and rounds down to the quotient. To get a floating-point result, cast one of the operands to float or, better, double:

double l = (double)number / (double)words * 100.0;

And so on. Since you shouldn’t need to change l once it’s defined, you might also want to declare it const within the scope where it is used. (Also, reconsider l as a variable name: it looks a lot like 1 or I in many fonts and isn’t very descriptive.)

Davislor
  • 14,674
  • 2
  • 34
  • 49
0

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?)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278