I am taking CS50x Harvard on edX, and I'm currently working on problem set 2, readability. I've compiled my code, and it should work to determine the reading level like it's supposed to. However, every time I run the program, no matter what I put in it gives back the answer before grade 1.
Where is the error?
Here's the code.
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(void)
{
// Get text from user
string text = get_string("Text: ");
// Start words on 1 because theres always 1 less space then words
int letters = 0;
int words = 1;
int sentences = 0;
// Count characters
for (int i = 0; i < strlen(text); i++)
{
// Check for letters
if (isalpha(text[i]))
{
letters++;
}
// Check for spaces
else if (text[i] == ' ')
{
words++;
}
else if (text[i] == '.' || text[i] == '?' || text[i] == '!')
{
sentences++;
}
}
float L = (float)letters / (float)words * 100;
float S = (float)words / (float)sentences * 100;
int index = (int)round(0.0588 * L - 0.296 * S - 15.8);
if (index > 16)
{
printf ("Grade 16+\n");
}
else if (index < 1)
{
printf ("Before Grade 1\n");
}
else
{
printf ("Grade %i\n", index);
}
}