0

In my program, the wordCount isn't always accurate and I can't for the life of me figure out why the phrase "lets go for a run.I like daffodils" in this order equals 7 words, when it's supposed to be 8. Also the float level that calculates reading level keeps returning a -15 float value regardless of what string the user gives the compiler, any advice would be appreciated, I'm not even particularly looking for answers just eyes on my logic, thanks

Here's the code:(edit formatting Im new to cs50)

#include <cs50.h>
#include <stdio.h>
#include<ctype.h>
#include <string.h>
//--------------------------------------------------------------------------------------------------------
float letterCount(string text);
int wordCount(string text);
float sentenceCount(string text);
    
//# of letters per 100 words
float letters = 0;
    
//# of words per 100 words
int words = 0;

//# of sentences per 100 words
float sentences = 0;

int main(void)
{
    string text = get_string("Please enter a text to scan the reading level:");

    printf("\n");
    printf("Text: %s\n",text);
    printf("\n");

    letterCount(text);
    wordCount(text);
    sentenceCount(text);

    //keep level down here so it doesnt touch letters or sentecnes values prematurely
    //Coleman-Liaux index formula for reading level
    float level = 0.0588 * letters - 0.296 * sentences - 15.8;
    printf("Grade Level: %f\n",level);
}

//count letters in the text entry
float letterCount(string text)
{
    int stringCount = strlen(text);

    for (int i = 0; i < stringCount; i++)
    {
         if (isalpha(text[i]))
         {
             letters++;
         }
    }

    letters = letters / 100.0;
    printf("%f\n", letters);
    return 0;
}

int wordCount(string text)
{
    int stringCount = strlen(text);


    if (stringCount > 0 && !isspace(text[0]))
    {
        words++;
    }

    for (int i = 0; i < stringCount; i++ )
    {
         if (isspace(text[i]) && !isspace(text[i-1]) && !isspace(text[i+1]))
         {
             words++;
         }
    }

    printf("%i\n", words);
    return 0;
}

float sentenceCount(string text)
{
    int stringCount = strlen(text);


    if (stringCount > 0 && !isalpha(text[0]))
    {
        sentences++;
    }

    for (int i = 0; i < stringCount; i++ )
    {
         if (isalpha(text[i]) && !isalpha(text[i-1]) && !isalpha(text[i+1]))
         {
             sentences++;
         }
    }

    sentences = sentences / 100.0;
    printf("%f\n", sentences);
    return 0;
}

1 Answer 1

1

One problem is that in every one of your functions you return 0 instead of returning the variable you spent time calculating in your function. Yes you changed them because they are globals but this is attrocious. If you are going to modify globals, make the function void, returning 0 always is pointless and you don't check the return value. Better still return the count and don't use globals This defeats the point of using functions.

The second problem is I have no idea what you are doing in trying to calculate words. Ask yourself what starts a word? what ends a word? I would say a letter starts a word. I would say a nonletter/non single quote mark ends a word (contractions and possesives). They might have made it simpler or more clear in the problem description or hints. Instead you seem to be focused on look ahead and look behind. But your not isspace check, does that mean that '.' is a word? "run.I" is two words but i'd bet your code somehow counts it as one word. Debug, use a print statement, why not print out the letter you think starts a word each time you start one? Same with sentences, you'll figure out quickly what you are missing.

Finally level keeps coming back as -15 because your math mixes types. Don't mix floats and int's unless you are careful with typecasting, and also because your math is flawed in all of your functions

You say you want letters per 100 words (and sentences per 100 words) but that's not what you calculate. You just divided by 100 which is incorrect. If there is 1 word, and 4 letters, you are saying there are .04 letters per 100 words which is not correct. You could argue there are 4 letters per 100 words (which is wrong but defendable) or you could say there are 400 letters per 100 words ( which is correct ). They show you the calculation to use in the problem background. Go back and reread the question and the problem. You need to divide by number of words and multiply by 100.

Strongly suggest you leave ALL count functions as modifying / returning ints. Do a cast to float and use float division to get your values for the equation seperately.

Every function should have 1 and only one purpose. Your functions purposes are to count the words/letters/sentences. Your desire to get a percentage / frequence per 100 words is a secondary business logic item that should be carried out seperately. This will also help you to test more cleanly.

1
  • if this answers your question, please mark it as accepted.
    – UpAndAdam
    Commented Mar 22 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .