pset2 readability always printing before grade 1 no matter what input

Viewed 34

I know this is a fairly newbie question so I'm sorry if the solution is painfully obvious to you guys. I've fully coded up pset 2 readability and it worked for printing out the number of letters, words and sentences for the user inputted text- I have since removed those print statement as they aren't needed for the pset (I just wanted to actually make sure the functions were returning something- they worked just fine).

I'm up to printing out the grade level now but no matter what text I input I only get before grade 1. I've already checked to see if I had anything wrong with my print statements and I can seem to find an issue there so I'm thinking that there may be an error in the calculation of the grade level itself- I've looked until my eyes have gone square and for the life of me I can not see anything wrong.

If someone could shed some light on my problem I would love to be saved the headache :), or even point me in the right direction so I get the learning. (also first time poster, long time lurkers so forgive me if anything is formatted incorrectly). Thank you all!

Here is my code:

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

// my functions to calculate letters, words
int count_letters(string text);
int count_words(string text);
int count_sentences(string text);

int main(void)
{
    string text = get_string("Text: ");
    int letters = count_letters(text);
    int words = count_words(text);
    int sentences = count_sentences(text);

    float calculation = (0.0588 * letters / words * 100) - (0.296 * sentences / words * 
    100) - 15.8;

    int index = round(calculation);

    if (index < 1)
    {
        printf("Before grade 1.\n");
    }
    else if (index >= 16)
    {
        printf("Grade: 16+.\n");
    }
    else
    {
        printf("Grade: %i.\n", index);
    }
}

int count_letters(string text)

{
    int letters = 0;
    for (int i = 0; i < strlen(text); i++)

        //((text[i] > 65 && text[i] < 90) || (text[i] > 97 && text[i] < 122))
        if (isalpha(text[i]))
        {
            letters++;
        }
    return letters;
}

int count_words(string text)
{
    int words = 0;
    for (int i = 0; i < strlen(text); i++)
        if isspace ((text[i]))
        {
            words++;
            words = words + 1;
        }

    return words;
}

int count_sentences(string text)
{
    int sentences = 0;
    for (int i = 0; i < strlen(text); i++)
        if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            sentences++;
        }
    return sentences;
}
2 Answers

I just wanted to actually make sure the functions were returning something- they worked just fine

Yes, the functions return "something" but is it the right thing? Suggest you add back the debug printf and look at the results carefully and critically. Start with the simplest text ("One fish. Two fish. Red fish. Blue fish."). 29 letters, 8 words, 4 sentences. What result is printed?

Inside of the function count_words you have an if statement, if statements need to have brackets, the line wouldn't make sense even if you had the brackets, so double-check the logic as well.

Related