#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
string text = get_string("Text: ");
int number = count_letters(text);
int number_of_words = count_words(text);
int sentences = count_sentences(text);
int L = (number / number_of_words) * 100;
int S = (sentences/number_of_words) * 100;
int index = (0.0588 * L) - (0.296 * S) - 15.8;
if (index < 16 && index > 1)
{
printf("Text: %s\n %i letters\n %i words\n %i sentences\n", text, number, number_of_words, sentences);
printf("L:%i\n", L);
printf("S:%i\n", S);
printf("Grade:%i\n", index);
printf("Grade %i\n", (int) round(index));
}
else if (index < 1)
{
printf("Before Grade 1\n");
}
else
{
printf("Grade: 16+\n");
}
}
//sentence counter
int sentences = 0;
int count_sentences(string text)
{
int lengths = strlen(text);
for (int i = 0; i < lengths; i++)
{
if(text[i] == '.' || text[i] == '?' || text [i] == '!')
{
sentences = sentences + 1;
}
else
{
sentences = sentences + 0;
}
}
return sentences;
}
//word counter
int words = 1;
int count_words(string text)
{
int length = strlen(text);
for (int j = 0; j < length; j++)
{
if(isspace(text[j]))
{
words = words + 1;
}
else
{
words = words + 0;
}
}
return words;
}
//letter counter
int letters = 0;
int count_letters(string text)
{
int len = strlen(text);
for(int i = 0; i < len; i++)
{
if(isalpha(text[i]))
{
letters = letters + 1;
}
else
{
letters = letters + 0;
}
}
return letters;
}
I used the debugging tool and I know that the problem is with the calculations, but the thing is I don't understand why, all the math should be correct and I'm also getting the correct number of letters, words, and sentences. I added all the printf's to be able to see the numbers myself in the terminal. Whenever I try the sentence, "There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy." I get grade 7 instead of 9.