0

What am I doing wrong?!

When I check my code with check50, I get this:

:( handles questions in passage
    expected "Grade 2\n", not "Grade 3\n"

So far, I haven't been able to fix this...tried Googling, changing the function for counting words several times...

Any help is appreciated!

My code below:

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

/* Prompt the user for some text
Count the number of letters, words, and sentences in the text
Compute the Coleman-Liau index: index = 0.0588 * L - 0.296 * S - 15.8
L is the average no of letters per 100 words in text; S is the average no of sentences per 100 words in text.
Print the grade level */

int countLetters(string text);
int countSentence(string text);
int countWord(string text);

int main(void)
{
    string text = get_string("Enter your text: ");

    int letter = countLetters(text);
    int sentence = countSentence(text);
    int word = countWord(text);

    float average_letter = (float) letter / (float) word * 100;
    float average_sentence = (float) sentence / (float) word * 100;

    float index = (0.0588 * average_letter) - (0.296 * average_sentence) - 15.8;
    int grade = ceil(index);

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

    return 0;
}
// function letters
int countLetters(string text)
{
    int count_letter = 0;

    for (int i = 0; text[i] != '\0'; i++)
    {
        if (isalpha(text[i]))
        {
            count_letter++;
        }
    }
    return count_letter;
}
// function sentences
int countSentence(string text)
{
    int count_sentence = 0;
    int is_sentence = 0;

    for (int i = 0; text[i] != '\0'; i++) {
        char current_char = text[i];

        if (current_char == '.' || current_char == '!' || current_char == '?' || current_char == ':')
         {
            if (is_sentence == 0)
            {
                is_sentence = 1;
                count_sentence++;
            }
        }
        else
        {
            is_sentence = 0;
        }
    }
    return count_sentence;
}
// function words
int countWord(string text)
{
    int count_word = 0;
    int is_word = 0;

    for (int i = 0; i < strlen(text); i++)
    {
        if (isalpha(text[i]))
        {
            if (i == 0 || !isalpha(text[i - 1]))
            {
                count_word++;
            }
        }
    }

    return count_word;
}

1 Answer 1

1

Introduction

Starting your question off with "My Code doesnt pass a check50 test" is NOT a good way to ask a question, approach the problem or get help.

Googling and changing your function randomly wont help you. What are you googling? The object is to learn to program/learn computer science not "to simply get the answer to this question and pass the level". So what are you googling for? googling for the answer breaks the honor code and defeats the point of the whole exercise. You have to know what's wrong to search and also know whats wrong before you change your code; and you don't know what is wrong other than that the final answer is wrong.

You need to apply the scientific method here and break it down. Figure out which method isn't working and then figure out why it isn't working and fix that method until it works. If you know that each of your count functions is working properly then the only possibility would be your combination of them into the index generation. However I can confirm that part is correct. The problem is your count functions. But you haven't communicated or demonstrated what you have done to verify that they are working. An approach of just googling and trying to change a single function (seemingly randomly) is like searching for a needle in a haystack only worse, you could be in the wrong haystack entirely. You need to know what to ask. You need to break the problem into smaller problems and make sure each subproblem is solved properly.

Test your code

There is sample data provided see "How To Test Your Code" section. Run your program on the sample data and possibly print out for each run the number of letters, sentences and words, in addition to the final grade and verify that they are correct. That should show you which function has a problem. and give you something to work with. check50 should not be your first and only attempt at testing

That said here are some things I found to help you.

  1. Instead of doing for loops bound by condition of text[i] != '\0' just use a i < strlen(text) check like you used in countWord in your other functions. This is a minor thing that won't change overall correctness but is a good practice.

  2. countSentence is broken. Two problems. Your starting value of is_sentence. How many sentences are there if the first character is a . or if the whole string is ...? Additionally your condition is wrong, : does not delineate a sentence according to the instructions.

  3. Your countWord function is broken. The instructions literally tell you as an example that sister-in-law should be counted as one word, you count it as three. You are simply asking if the prior character wasn't a letter, is that sufficient? (yes the check for i == 0 before your lookback is indeed correct). you also define but don't use is_word.

1
  • if this answers your question please mark as accepted thank you
    – UpAndAdam
    Feb 20 at 16:13

You must log in to answer this question.

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