Strange syntactic error in the code.

Feb 6, 2013 at 7:12pm
Hi Everyone,

I'm a total starter with C. That is a simple hometask that I'm trying to code but get stuck with a syntactic error when trying to create a loop.

What I am trying to achieve is to have the program check whether a Boolean condition is false and then to re-evaluate the expression (basically, it should be endlessly repeating the same action till a condition is met: the user should type in S an operator to proceed with calculations):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdbool.h>

int main (void)

{
    float number;
    float accumulator;
    char operator;
    bool condition;
    
    condition = false;
    
     while (condition == false)
   { 
    printf ("Enter the accumulator: ");
    scanf ("%f %c\n", &accumulator, &operator);
    
    if (operator != 'S')
    printf ("The accumulator should be entered before proceeding with calculations. Please use S character to set an accumulator.");
    condition = false;
}
    else 
    condition = true;


When trying to compile this (given the return statement and the closing quote are at the end) i am receiving this error on line 23:

syntax error before "else". I've copied the part which the error pertains to:
1
2
3
4
5
if (operator != 'S')
    printf ("The accumulator should be entered before proceeding with calculations. Please use S character to set an accumulator.");
    condition = false;
}
    else 


I've checked the formatting of the code and it looks valid to me (even though I'm totally new). Could anyone please point me at what I might have missed. I tried to modify the code multiple times but can't get around that error and still unsure what causes it.

Thanks for your help!
Last edited on Feb 6, 2013 at 7:17pm
Feb 6, 2013 at 7:21pm
operator is a keyword, you can't name a variable operator
Feb 6, 2013 at 7:21pm
You're missing braces for the if clause. You can only ommit them if the if clause has one statement, your's has two.
Feb 6, 2013 at 7:42pm
I've changed the 'operator' to a more appropriate name ('op') and added the braces for the if statement but still getting the error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    float number;
    float accumulator;
    char op;
    bool condition;
    
    condition = false;
    
     while (condition == false)
 {  
    printf ("Enter the accumulator: ");
    scanf ("%f %c\n", &accumulator, &op);

    if (op != 'S')
   { printf ("The accumulator should be entered before proceeding with calculations. Please use S character to set an accumulator.");
    condition = false;}
}
    else 
    condition = true;


Still missing something but can't figure out what and where.
Feb 6, 2013 at 7:50pm
Well now you have that else outside of the while scope, but the if that belongs with it is in the while scope. You need to move that bracket ending the while block to underneath your else block.
Feb 6, 2013 at 7:58pm
Done, still getting the same error. I've tried quite a few alterations now and revised the syntax but no luck yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdbool.h>

int main (void)

{
    float number;
    float accumulator;
    char op;
    bool condition;
    
    condition = false;
    
     while (condition == false)
 {  
    printf ("Enter the accumulator: ");
    scanf ("%f %c\n", &accumulator, &op);

    {if (op != 'S')
    printf ("The accumulator should be entered before proceeding with calculations. Please use S character to set an accumulator.");
    condition = false;}

    else 
    condition = true;
}
Feb 6, 2013 at 8:00pm
Stop changing multiple things at a time. Now your if bracket is in the wrong spot. Here's the format:

1
2
3
4
5
6
7
8
9
if(condition) {
statements...
...
...
}
else {
statements...
...
}
Feb 6, 2013 at 8:01pm
I've also tried to put the closing brace for the if statement after else clause, but the error still persists:

1
2
3
4
5
6
7
8
9
10
11
12
     while (condition == false)
 {  
    printf ("Enter the accumulator: ");
    scanf ("%f %c\n", &accumulator, &op);

    {if (op != 'S')
    printf ("The accumulator should be entered before proceeding with calculations. Please use S character to set an accumulator.");
    condition = false;

    else 
    condition = true;}
}
Feb 6, 2013 at 8:09pm
Thanks for the hint! I really used the wrong syntax. It's working now! ))

Will be more careful next time with the braces ))

Thanks!
Topic archived. No new replies allowed.