简体   繁体   中英

CS50 Readability Calculation issue (C)

I have been working on this for a week now and everything is working fine with my program except when I am trying to calculate the grade using the index formula that they provided. No matter what is inputted I am getting a negative number resulting in printing "before grade 1". I have looked around and saw that somebody else was getting this issue and they fixed it by changing "100" to "100.0" in their letters and sentences average calculation. This unfortunately did not work for me. I have tried other methods people have suggested, I have tried changing the variable datatypes, removing the rounding, and even tried different ways of calculating the averages but so far I have not gotten anything to calculate the index correctly. Could anybody please help? for example, if this text is inputted..

"It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him."

it should output "Grade 10" instead it is outputting "before grade 1"

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

int main(void)
{

    float sentenceAVG = 0.0f;
    float lettersAVG = 0.0f;
    int index = 0.0588 * lettersAVG - 0.296 * sentenceAVG - 15.8;

    int sentences = 0;
    int letters = 0;
    int words = 1;
    int textLength = 0;
    int indexRounded = 0;

    string userText = get_string("Text: ");
    textLength = strlen(userText);

    for(int i = 0; i < textLength; i++)
    {
        char x = userText[i];

        if (isalpha(x))
        {
            letters += 1;

        }

        if ((x == '.') || (x == '!') || (x == '?'))
        {
            sentences += 1;
        }

        if (isspace(x))
        {
            words += 1;
        }
    }


    lettersAVG = 100.00 * letters / words;
    sentenceAVG = 100.00 * sentences / words;
    indexRounded = round(index);

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

} ```

In this line:

int index = 0.0588 * lettersAVG - 0.296 * sentenceAVG - 15.8;

lettersAVG and sentenceAVG are both defined as 0.0 in the two lines prior. index is therefore 0, the integer. To fix this, just place the line above after you change the values of lettersAVG and sentenceAVG . Also, make sure that index is a float before being rounded, because if not the identifier int will automatically truncate it for you, which is bad. BTW, it would be useful if you learned some debugging techniques, such as using print statements to check the value of index throughout runtime, telling you where the problem is.

Your code should look something like this:

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

int main(void)
{

    float sentenceAVG = 0.0f;
    float lettersAVG = 0.0f;

    int sentences = 0;
    int letters = 0;
    int words = 1;
    int textLength = 0;
    int indexRounded = 0;

    string userText = get_string("Text: ");
    textLength = strlen(userText);

    for(int i = 0; i < textLength; i++)
    {
        char x = userText[i];

        if (isalpha(x))
        {
            letters += 1;

        }

        if ((x == '.') || (x == '!') || (x == '?'))
        {
            sentences += 1;
        }

        if (isspace(x))
        {
            words += 1;
        }
    }


    lettersAVG = 100.00 * letters / words;
    sentenceAVG = 100.00 * sentences / words;
    float index = 0.0588 * lettersAVG - 0.296 * sentenceAVG - 15.8;
    indexRounded = round(index);

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

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM