1

I have an issue in the Readability part of the CS50 course that I've tried to troubleshoot forever, but I can't seem to get anywhere. I'm pretty sure the issue here is my index function that's not calculating it right or my l and s being an issue, but I'm lost at this point, and I'm hoping someone can help.

After having debugged multiple parts of the code, I've concluded that the issue must be in this part of the code:

//Calculate the average number of letters and sentences per 100 words
float l = (letter_count / word_count) * 100;
float s = (sentence_count / word_count) * 100;

//Calculating the Coleman-Liau index
int index = round(0.0588 * l - 0.296 * s - 15.8);

Full code snippet for context:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

//Declaring all the functions
int count_letters(string text_input);
int count_words(string text_input);
int count_sentences(string text_input);

int main (void)
{
    //Getting a string to measure
    string text_input = get_string("Text: ");

    //Calling all functions
    int letter_count = count_letters(text_input);
    int word_count = count_words(text_input);
    int sentence_count = count_sentences(text_input);

    //Calculate the average number of letters and sentences per 100 words
    float l = (letter_count / word_count) * 100;
    float s = (sentence_count / word_count) * 100;

    //Calculating the Coleman-Liau index
    int index = round(0.0588 * l - 0.296 * s - 15.8);

    //Printing results
    if (index < 1)
    {
        printf("Before Grade 1");
    }
    else if (index > 16)
    {
        printf("Grade 16+");
    }
    else
    {
        printf("Grade %i", index);
    }
    printf("\nIndex %i\n", index);
}

//Counting letters vai a loop
int count_letters(string text_input)
{
    int letters = 0;

    for (int i = 0, len = strlen(text_input); i < len; i++)
    {
        if (isalpha(text_input[i]))
        {
            letters++;
        }
    }
    return letters;
}

//Counting words by counting spaces and adding 1 to the end.
int count_words(string text_input)
{
    int words = 1;

    for (int i = 0, len = strlen(text_input); i < len; i++)
    {
        if (text_input[i] == ' ')
        {
            words++;
        }
    }

    return words;
}

//Counting number of '.', '?' and '!' to count sentences
int count_sentences(string text_input)
{
    int sentences = 0;

    for (int i = 0, len = strlen(text_input); i < len; i++)
    {
        if (text_input[i] == '.' || text_input[i] == '!' || text_input[i] == '?')
        {
            sentences++;
        }
    }

    return sentences;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mathias Laursen
  • 23
  • 1
  • 1
  • 4
  • Please focus your question by describing your observations. showing sample input, desired output, the output you gett for several different cases, i.e. aim for [ask]. Try debugging ( https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ ). The number of users who can and want to help you will probably increase noticably, if you can demonstrate your problem with a [mre] without cs50 headers. – Yunnosch Apr 25 '21 at 16:46
  • Thank you for the feedback. I've reformatted it now to make it easier to understand (hopefully). – Mathias Laursen Apr 25 '21 at 18:39
  • What makes you think that the issue is in that specific code part? I was not complaining about formatting. My first comment still applies in full. – Yunnosch Apr 25 '21 at 18:55
  • Though I would really like to teach you asking a question in a useful and answerable way, I admit that I can guess at your problem. So I wonder whether ... Does this answer your question? https://stackoverflow.com/questions/27674295/why-do-we-separately-cast-to-float-in-an-integer-division (fellow users, I already close-voted for lack of focus, somebody please add a close-vote for duplicate). – Yunnosch Apr 25 '21 at 18:59
  • I've debugged the code to ensure that the counting functions are correct, and they are. So the issue must be in that snippet of code. I've tried to also write float in front of it, but haven't been able to get the right answers. Why is this a duplicate though? I've not been able to find that answer anywhere. – Mathias Laursen Apr 25 '21 at 19:16
  • Please describe what wrong behaviour your observe. Or look through your question and tell me where you have already described that. Perhaps I just fail to see where you state what misbehaviour you observe. Also please confirm that none of the solutions in the question I linked, e.g. basically `(letter_count / (float) word_count)` and similar for the other division, does not help. Because in that case I guessed incorreclty and instead it is really you who has to describe what exactly is going wrong. What do you get? What do expect to get? For which input? – Yunnosch Apr 25 '21 at 20:55
  • Here is one of the many questions on an extremely similar problem, this one with actually the same cs50 assignment. Please read it and try to see the similarity of the the mistake made there, the solution proposed there and your code. https://stackoverflow.com/questions/63586606/bug-in-cs50-readability-pset2?rq=1 Mayb it is easier for you to spot the similarity there. – Yunnosch Apr 25 '21 at 21:01
  • 1
    Following up on Yunnosch's suggestion, when you do (e.g.): `float l = (letter_count / word_count) * 100;` you are doing an _integer_ divide. You probably want floating point. Just because `l` is `float` will _not_ force the expression to `float`. As an alternative, try (e.g.): `float l = (100.0f * letter_count) / word_count;`. The linked answer is an exact match for your issue. – Craig Estey Apr 25 '21 at 21:28

0 Answers0