else without a previous if

Hi, I have a problem with my code. It says "else without a previous if".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   if (number == 0)
    {
        cout << "Zero\n";

    }
    {
        h = number/100;
        number = number%100;
        if (number>10 && number<20)
        {
            t = 0;
            o = 0;
            r = number%10;
        }

    }
    else
    {
        t = number/10;
        number = number%10;
        o = number;
    }


I tried doing what I can but it still says the error.
Line 17 has an else but no if before it.
My line 1 has an if.
Yes, but there is another block separating that if from your else starting on line 6.
1
2
3
4
5
6
if{ //Start of if
}
{ //Start of another code block; indicates previous if statement is done
}
else{ //an else that is presumed to belong to a separate if-else statement
}


Either you're missing an else if on line 6 or that code block is in the wrong place.
Last edited on
This is what I have
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
int main()
{
    int number;
    int h;
    int o;
    int r;
    int t;

    cout << "Enter a number: ";
    cin >> number;
    cout << "\nNumber: " << number;
    cout << "Roman number: ";
    if (number == 0)
    {
        cout << "Zero\n";

    }
    {
        h = number/100;
        number = number%100;
        if (number>10 && number<20)
        {
            t = 0;
            o = 0;
            r = number%10;
        }

    }
    else
    {
        t = number/10;
        number = number%10;
        o = number;
    }

    switch(h)
Did you mean to have that else block on line 29 at line 26/7 instead (and have an else at line 18) ?
Yeah, how do I fix that?
Remember that all else and all else if must following immediately after an if or else if block:
1
2
3
4
5
6
7
8
9
10
11
if(blah blah){
}else if{
}else{
}

if(blah blah){
   if(blah){
   }else{
   }
}else{
}


Any separate code block in between will break that chain, for example:
1
2
3
4
5
6
if(blah blah){
}
{ //Separate code block
}
else{ //Now this belongs to a separate if-else statement, where the if is missing
}


For your particular code, just move the block at line 29 into the block that starts on line 18 (and add else):
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    if (number == 0)
    {
        cout << "Zero\n";

    }
    else{ //This else will correspond to the if on line 13
        h = number/100;
        number = number%100;

        if (number>10 && number<20)
        {
            t = 0;
            o = 0;
            r = number%10;
        }
        else{//This one will correspond to the if on line 22
            t = number/10;
            number = number%10;
            o = number;
        }
    }


I'm not sure I understand why you have lines 22 to 27 (relative to my revision) in what appears to be a decimal to roman number conversion program. There is nothing special about roman numbers in the (10, 20) range.
Last edited on
Topic archived. No new replies allowed.