-4

I have following code in C language:

#include <stdio.h>
#define NOERROR 0

int c;
int parentheses, brackets, braces;

void checkErrors(void);

int main()
{
    extern int parentheses, brackets, braces;
    extern int c;

    parentheses=0; brackets=0; braces=0;

    while ((c = getchar()) != EOF){
        if (c == '(')
            {++parentheses;}
        else if (c == ')')
            {--parentheses;}
        else if (c == '[')
            {++brackets;}
        else if (c == ']')
            {--brackets;}
        else if (c == '{')
            {++braces;}
        else if (c == '}')
            {--braces;}
    checkErrors();
}

void checkErrors(void)
{   
    extern int parentheses, brackets, braces;
    extern int c;

    if (parentheses != NOERROR){
        printf("You have missed some parentheses"); 
        }

    if (brackets != NOERROR){
        printf("You have missed some brackets");
        }

    if (braces != NOERROR){
        printf("You have missed some braces");
        }
}

I receive this error: expected declaration or statement at end of input on line 55 (when main function ends) Why is this happening? I didn't miss any brackets.

Thanks

  • 1
    "I didn't miss any brackets" - um, yes you did. Your code, aligned by brackets, [see here](http://pastebin.com/Zshb6rT4). Whether it is the closing bracket of the while loop or the closing bracket of `main()` I leave to you to decide, but your closing statement in your question is false regardless. – WhozCraig Jul 06 '14 at 18:13

3 Answers3

2

The statement after while is invalid. Before the else if there shall be if

while ((c = getchar()) != EOF){
    else if (c == '(')

Also the while statement shall have the closing brace.

Place closing braces after these statements

      else if (c == '}')
            {--braces;}
   } // <== 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You never closed the while loop in your main().

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

While loop is not closed by }. Use good editor.

Use Switch case as it is more readable for lots of condition.

while ((c = getchar()) != EOF){
switch (c)
      {
                case '(':
                    ++parentheses;
                    break;
                case ')':
                    --parentheses;
                    break;
                case '[':
                    ++brackets;
                    break;
                case ']':
                    --brackets;
                    break;
                case '{':
                    ++braces;
                    break;
                case '}':
                    --braces;
                    break;
      }
}
loop
  • 9,002
  • 10
  • 40
  • 76