I'm struggling with PSet 2 Readability. I don't want to cheat the answers because I am genuinely trying to learn how to think better and improve my logic.
I so far have been able to get the letter count, word count and sentence count. I created a function that is intended to run the formula. My function is supposed to return the value of "cfindex"
When I printf the count for letter, word, sentence this seems all correct but my printf for the function returns -15 no matter the sentence I input. Guidance is appreciated!
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <ctype.h> //to count character
#include <string.h>
int formula(int L, int S);
int hundredWordCount = 100;
int main(void)
{
//Get string
string text = get_string ("Text: \n");
int stringCount = strlen(text); //(text) passes the above string variable into strlen function
int letterCount = 0; //letters for Coleman-Lau formula
int sentenceCount = 0; //Sentences for Coleman-Lau formula
int words = 1;
//count letters
for (int letters = 0; letters < stringCount; letters++)
{
//isalpha checks only for alphabetic characters so if it is a alphabetic character, iterate L++
if(isalpha(text[letters]))
{
// text[letters] =
letterCount++; //works
}
if(isspace(text[letters]))
{
//Each space will determine this is a word
words++; //works
//Problem with above is it needs to check if there's a letter after
//Check for a letter and increment
//I also need to calculate per 100 words
if (words >= 100)
{
hundredWordCount+=100;
return hundredWordCount;
}
}
//To determine sentences, I need to identify . ! ?
if(text[letters] == '.' || text[letters] == '!' || text[letters] == '?')
{
sentenceCount++; //works
}
//If I use this calculation, it will zero out my letters and my sentences
//Per 100 words calculation
// letterCount = letterCount / hundredWordCount;
// stringCount = sentenceCount / hundredWordCount;
}
//testing printf values
printf("Letters: %i\n Words: %i\n Sentences: %i\n StringCount: %i\n WordCount: %i\n Index: %.1d\n", letterCount, words, sentenceCount, stringCount, hundredWordCount, formula(letterCount/100, sentenceCount/100)); //this printf for formula always returns -15
int formula(int L, int S)
{
double cfindex;
//coleman lau index
cfindex = 0.0588 * L - 0.296 * S - 15.8;
return cfindex; //output
}