Strange syntactic error in the code.

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
operator is a keyword, you can't name a variable operator
You're missing braces for the if clause. You can only ommit them if the if clause has one statement, your's has two.
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.
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.
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;
}
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...
...
}
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;}
}
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.