Need help!
- Newbie alert over here - Readability program compiles fine, and print a return message, but I can't figure out why i keep getting the wrong grade number. Seems letter, word and sentence counts are running ok, so i thought it had to be the math (not my strong suit) but can not find the issue... What am i missing?
Here's my code
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
// PART 1 Get user to input text
string text = get_string("Text: \n");
//create variables
int letters = 0;
int words = 1;
int sentences = 0;
int n = strlen(text);
//PART 2 Start loop verifying characters type
for (int i = 0; i < n; i++ ) {
//verfity if letter either upper or lower case, then add 1 letter
if(isalpha(text[i]))
{
letters++;
}
//check if space char, then add one word
else if(text[i] == ' ')
{
words++;
}
//check if end of sentence punctuation, then add 1 sentence
else if(text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentences++;
}
}
//PART 3 calculate averages to use index
//create variables that can use decimal numbers
float l = letters / words * 100;
float s = sentences / words * 100;
float index = 0.0588 * l - 0.296 * s - 15.8;
//round index to closest integer
int coLiIndex = round(index);
//PART 4 Output print results with different messages
if (coLiIndex >= 1 && coLiIndex <= 16)
{
printf("Grade: %i\n", coLiIndex);
printf("%d\n", letters);
printf("%d\n", words);
printf("%d\n", sentences);
}
if (coLiIndex > 16)
{
printf("Grade 16+\n");
printf("%d\n", letters);
printf("%d\n", words);
printf("%d\n", sentences);
}
else if(coLiIndex < 1)
{
printf("Before Grade 1\n");
printf("%d\n", letters);
printf("%d\n", words);
printf("%d\n", sentences);
}
}