2

I've tried everything but I can't figure out the bug in my code.

Getting the user inupt and counting the letters

int main(void) 
{
int letters = 0;

//Getting user input
string text = get_string("Text: ");

//Counting the letters
for (int i = 0; i < strlen(text); i++) 
{
    if (isalpha(text[i])) 
    {
        letters++;
    }

}

counting the words and the sentences

int words = 1;

//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++) 
{
    if ((isspace(text[i])) && (isalpha(text[i+1])) ) 
    {
        words++;
    }
}


int sentences = 0;

//Checking the symbols and counting the sentences
for (int i = 0; i < strlen(text); i++) 
{
    if (text[i] == '.' || text[i] == '!' || text[i] == '?') 
    {
        sentences++;
    }
}

And then applying the formula

double L = 100.0 * letters / words;
double S = 100.0 * sentences / words;

double index = 0.0588 * L - 0.296 * S - 15.8;

int trueIndex = round(index);

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

}

It gives me this error: expected "Grade 8\n", not "Grade 9\n". I know it has something to do with how I handled floats but I don't understand what's wrong

Tamara N
  • 73
  • 7
  • 1
    What are the values of `letters` and `words` and `sentences`. – Barmar May 29 '20 at 20:43
  • You have an unmatched `}` at the end. – Barmar May 29 '20 at 20:43
  • I doubt this will make a difference, but floating-point literals have type `double` instead of `float`, so as a first step try adding `f` to the end of your floating-point literals, i.e., `L = 100.0f * letters / words`, `0.0588f * L - 0.296f * S - 15.8f`, etc. – John Bode May 29 '20 at 20:53
  • The value of letters, words and sentences are integers. I think the problem has to be with the index formula because the code checks all the point except grade 8- grade9 – Tamara N May 29 '20 at 20:53
  • @TamaraN: Not the types, the *values* - what combination of values are you expecting to evaluate to `8` given the index formula? – John Bode May 29 '20 at 20:54
  • @JohnBode Alternatively, declare all the variables to be `double`. It's rarely necessary to limit precision with `float`. Also, isn't the implementation allowed to do all the intermediate computations using higher precision, so forcing everything to `float` doesn't ensure that it will limit the precision of the calculation? – Barmar May 29 '20 at 21:03
  • Edit the post to show a [mre]. – Eric Postpischil May 29 '20 at 21:18
  • Can you give us sample of input and its output? – NAND May 30 '20 at 00:54
  • Have you found answer you looking for yet? – Gribek May 30 '20 at 08:06
  • @NAND Input: Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" Correct output would be grade 8, but I get grade 9 – Tamara N May 30 '20 at 18:43
  • @Gribek I haven't... – Tamara N May 30 '20 at 18:44

2 Answers2

0

Try to delete (isalpha(text[i+1]) part from counting words. isalpha() will return true for alphabetical characters only, i.e. a - z , A - Z. For quotation mark will return false and won't count such word.

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"

//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++) 
{
    if (isspace(text[i]))
    {
        words++;
    }
}
Gribek
  • 191
  • 6
  • @TamaraN Glad I can help. Please consider [Accepting answer](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is, of course, no obligation to do this :-) – Gribek May 30 '20 at 19:24
0

Maybe this block is a making troubles:

//Checking the spaces and counting the words
for (int i = 1; i < strlen(text); i++) 
{
    if ((isspace(text[i])) && (isalpha(text[i+1])) ) 
    {
        words++;
    }
}

Particulary (isalpha(text[i+1])) with that condition you assume, that after whitespace will come alphabetical char. But in test cases there are sentences with quotation mark (") and that does not count as an alphabetical. Therefore, you will not count this as a word.

The test sentence, that should be grade 8, but you return 9, is:

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?

I tried to run that program with your condition, and it really returns 9 at that sentence. TL;DR: Remove check for alphabetical after whitespace, and you are golden.

Pavel S
  • 11
  • 2