CS50 weeks 2 - readability.c woes.. Can't figure out how to fix the code

175 Views Asked by At

In week 2 of CS50, having problems creating the readability program. No idea how to fix my code at the moment. This is what I get when checking the code: https://submit.cs50.io/check50/882c9f1f8fbbb109d3ef495101233c72a995b880 This is all new to me so would appreciate some help from any seasoned C programmer out there. Thank you in advance. Below is the code.

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

int main(void)
{

    /* Get the text fromn the user */
    string text = get_string("Text: ");

    /* Initiate 3 integers to count words,letters and sentences*/
    int letters = 0, words = 0, sentences = 0;

    /* Loop to iterate through the text and count the values initated above */
    for (int i = 0; i < strlen(text); i++)
    {
        if (isalpha(text[i]))
        {
            letters++;
        }

        if (isspace(text[i]))
        {
            words++;
        }

        if (text[i] == '.' ||  text[i] == '!' || text[i] == '?')
        {
            sentences++;
        }

    }

    /* Initiate floats to use with Coleman-Liau index */
    float l = letters / words * 100, s = sentences / words * 100, index = 0.0588 * l - 0.296 * s - 15.8;

    /* round the float into the closest integer*/
    int grade = round(index);

    /* print the grade after text analysis*/
    if (grade >= 1 && grade <= 16)
    {

        printf("Grade %i\n", grade);
    }

    else
    {
        if (grade < 1)
        {

            printf("Before Grade 1\n");
        }

        if (grade > 16)
        {

            printf("Grade 16+\n");
        }

    }

}

0

There are 0 best solutions below