Parse Error before 'else' - Can't seem to figure it out.

Hi,
I'm new to C++, currently in the process of trying to teach myself.
I figured a good place to start was to write some simple math oriented
programs. I already wrote one to determine if a number is Even/Odd, and I wrote another to determine the square root of a number. Anyways, my current project is a program that will list all the divisors for a given number. I even figured out a technique to determine divisors for a negative number. I've finished my code, but I get a parsing error that I just can't seem to figure out. I've been staring at my work for about an hour now with no progress. I get the parse error on lines 25 & 36. So any help would be greatly appreciated :)

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <math.h>

int main()
{
    int testNum;
    int counter = 1;
    std::cout << "Divisor Finder\n\n";
    std::cout << "This program will list all the possible divisors of a given number!\n";
    std::cout << "What number would you like to test?\n\n";
    std::cin >> testNum;
    std:: cout << "\n\n";
    
    if (testNum >= 0)
        while (counter <= testNum)
                if (testNum % counter == 0)
                {
                                std::cout << "The number " << counter << " is a divisor of " << testNum <<".\n"; 
                                ++counter;
                }
                else
                                ++counter;
        else
                return 0;
    else
    {
        counter = fabs (testNum); // set counter to testNum's absolute value
        while (counter >= testNum)
                if (testNum % counter == 0 && counter != 0)
                {
                                std::cout << "The number " << counter << " is a divisor of " << testNum <<".\n";
                                --counter;
                }
                else
                                --counter;
        else
                return 0;
    }
    
    return 0;
}
Last edited on
You can only pair an 'else' with an 'if'. You can't pair it with a 'while'.

your indentation shows the problem quite nicely:

1
2
3
4
5
6
7
8
9
10
11
    if (testNum >= 0)
        while (counter <= testNum)     //  this 'while'
                if (testNum % counter == 0)
                {
                                std::cout << "The number " << counter << " is a divisor of " << testNum <<".\n"; 
                                ++counter;
                }
                else
                                ++counter;
        else             // is followed by this 'else'
                return 0;


This is not legal. I don't know what you're trying to accomplish by that anyway.

Also, I'd recommend you put braces around control blocks that are multiple lines. It's true that you don't technically need braces for those while loops because they consist of a single if/else chain, I'd recommend you put braces there anyway for claritiy's sake.
Last edited on
You are missing { } around a couple of statements.

if on 14, while on 15, and the while on 28 (based on your indentation).
Hi :-)

I think this is an incarnation of the "dangeling else" problem. For minimal example consider the following code:

1
2
3
4
5
6
7
8
9
if (a)
    if (b)
    {
        // some code
    }
// Does the statement started with if (a) end here?
else
    // some code
// or here? 


There are two ways to interpret this code. The first is:
1
2
3
4
5
6
7
8
9
10
11
if (a)
{
    if (b)
    {
        // some code
    }
}
else
{
    // some code
}

Here the else belongs to the first if.

The second interpretation is
1
2
3
4
5
6
7
8
9
10
11
if (a)
{
    if (b)
    {
        // some code
    }
    else
    {
        // some code
    }
}

Here the else belongs to the inner if. Maybe this example is to small to give the same error, as some compilers catch this.

The easiest way to circumvent this kind of problem is to always use { and }, so it is unambiguously clear what you mean.

For more info, try googling for "dangeling else".

EDIT: I now see that this is not a dangeling else, but nonetheless its good to know about it :-)

Greetings, TheBear
Last edited on
@Disch

Oh! I guess I got a bit confused with if & while loops. Indeed I did put an else on the end of a while loop, but I figured they belonged there... guess I should read a bit more. I'll make sure to add braces to my blocks of code that are multiple lines in length in the future, thanks.

@firedraco

Thanks, i'll be sure to toss 'em in.
@TheBear

Your example explains the dangling else pretty clearly to me. Thanks for the advice :)
Topic archived. No new replies allowed.