'CS50- Readability prints only "Before Grade 1"
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
int main(void) { string Text = get_string("What is the text? \n");
int letters = 0;
int words = 1;
int sentences = 0;
for (int i = 0, n = strlen(Text); i < strlen(Text); i++)
{
if (isalpha(Text[i]))
{
letters++;
/*printf("%i", letters);*/
}
{
if (isspace(Text[i]))
{
words++;
printf("%i", words);
}
else if (Text[i] == '.' || Text[i] == '!' || Text[i] == '?')
{
sentences++;
printf("%i", sentences);
}
}
//use Coleman-Liau Index
float calculation = (0.0588 * letters / words * 100) - (0.0296 * sentences / words * 100)
- 15.8;
int index = round(calculation);
if (index < 1)
{
printf("Before Grade 1\n");
return 0;
}
else if (index >= 16)
{
printf("Grade 16+\n");
return 0;
}
else
{
printf("Grade %i\n", index);
}
}
}`
If I remove the return function, it prints out numerous grades which are all different. If I print out with returns included, no matter the Text. it prints out : Before Grade 1. I don't know what I'm doing wrong.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
