0

Im trying to compile the program but keep getting this error message below: Can someone tell me what this means please. The carat arrow is supposed to be pointing at "char" and "ch" that is where they say the error is.

readability.c:36:9: error: expected expression
         char ch = s[i];
         ^

readability.c:38:17: error: use of undeclared identifier 'ch'
    if (isalpha(ch))
                ^
     

Below is the part of the program that has the error:

char ch = s[i];

if (isalpha(ch))
{
    letters++;
}
if (isspace(ch))
{
    words++;
}
if (ch == '.' || ch == '!' || ch == '?')
1
  • What is the carat (^) pointing to in the erros? Very significant information. How is s declared? Consider editing the question and including the first 8 lines of the code, because I don't see enough to go on. The second error on line 17 is presumably because the declaration of ch on line 9 failed. – DinoCoderSaurus Jan 9 at 19:58
2

I believe the problem has nothing to do with ch. After editing the question, and rereading everything carefully, including the raw post in edit, here's where I see the problem.

The first error is this:

readability.c:36:9: error: expected expression
         char ch = s[i];
         ^

That "expected expression" error usually happens because there's a stray curly brace, semicolon or some other structural problem at the end of the previous line. Thus, the carat would point at "char", the first thing it finds after the real error, and not "ch".

Because of this error, it fails to process the declaration of ch. The other errors follow from this.

This is why it's important to process compile errors in order. One error can trigger a dozen more false errors. Basing analysis on those errors can lead you to chasing a nonexistent problem.

I suspect that if the previous few lines were added, we would find the problem there. If this doesn't answers your question, please edit the question and post the code from beginning to about line 50.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance ;-)

1
  • Thank you the problem was that i was missing a curly brace after the for loop and at the end – Lovely Locs Jan 9 at 23:34
0

I was missing a set of curly braces after the for loop: I added a curly brace right below the for loop and another one at the end.

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

        char ch = s[i];

        if (isalpha(ch))
        {
            letters++;
        }
        if (isspace(ch))
        {
            words++;
        }
        if (ch == '.' || ch == '!' || ch == '?')
        {
            sentences++;
        }
    }

You must log in to answer this question.

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