Else without a previous if

Hi! How are you? Im starting on C++ and I had this problem before and it was because of the braces, I used them this time but it keeps telling me that i have elses without a previous if, what can I do? thanks

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>

using namespace std;

int main()
{{

    float peso, tarifa;
    int anio, clase;


 cout << "Ingrese el anio del modelo de su vechiculo" << endl;
    cin >> anio;

    cout << "Ahora ingrese el peso de su vehiculo"<<endl;
    cin >> peso;

    {
        if (anio<=1970)
                {

                           {if (peso<2700)
                           clase=1;
                           tarifa=16.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }{
                           else if ((peso>=2700)&&(peso<=3800))
                           clase=2;
                           tarifa=25.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                           {
                           else if (peso>3800)
                           clase=3;
                           tarifa=46.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }

                    }
        else if ((anio>=1971)&&(anio<=1979))
                    {
                           {
                           if (peso<2700)
                           clase=4;
                           tarifa=27.00;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                           {
                           else if ((peso>=2700)&&(peso<=3800))
                           clase=5;
                           tarifa=30.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                           {
                           else if (peso>3800)
                           clase=6;
                           tarifa=52.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }

                    }

        else if (anio>=1980)
                    {
                           {
                           if (peso<3500)
                           clase=7;
                           tarifa=19.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                           {
                           else if (peso>=3500)
                           clase=8;
                           tarifa=52.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                    }


                        }
    }
    return 0;
}
Well ... there are too many braces - for example at line 6 there are two {{ where only one is needed, and at line 18 the open brace is not needed.

They are at least in matching pairs, which is good.

However with regard to the if-else statements I believe the open and close braces are in the wrong place.

I only worked through the first block of code, if (anio<=1970) the rest should be similar, but I omitted it here. You should be able to follow the same pattern for the other sections.
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
42
43
44
45
46
47
#include <iostream>

using namespace std;

int main()
{
    float peso, tarifa;
    int anio, clase;

    cout << "Ingrese el anio del modelo de su vechiculo" << endl;
    cin >> anio;

    cout << "Ahora ingrese el peso de su vehiculo"<<endl;
    cin >> peso;

    if (anio<=1970)
    {
        if (peso<2700)
        {
            clase=1;    
            tarifa=16.50;
            cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
        }
        else if ((peso>=2700)&&(peso<=3800))
        {
            clase=2;
            tarifa=25.50;
            cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
        }
        else if (peso>3800)
        {
            clase=3;      
            tarifa=46.50;
            cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
        }
    }
    else if ((anio>=1971)&&(anio<=1979))
    {
        // etc.
    }
    else if (anio>=1980)
    {
        // etc.
    }

    return 0;
}


Actually, since all the cout statements are the same, you only need one, at the end:
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
42
43
44
45
46
#include <iostream>

using namespace std;

int main()
{
    float peso, tarifa = 0.0;
    int anio, clase = 0;

    cout << "Ingrese el anio del modelo de su vechiculo" << endl;
    cin >> anio;

    cout << "Ahora ingrese el peso de su vehiculo"<<endl;
    cin >> peso;

    if (anio<=1970)
    {
        if (peso<2700)
        {
            clase=1;    
            tarifa=16.50;
        }
        else if ((peso>=2700)&&(peso<=3800))
        {
            clase=2;
            tarifa=25.50;
        }
        else if (peso>3800)
        {
            clase=3;      
            tarifa=46.50;
        }
    }
    else if ((anio>=1971)&&(anio<=1979))
    {
        // etc.
    }
    else if (anio>=1980)
    {
        // etc.
    }
    
    cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;

    return 0;
}
Last edited on
Sorry to be blunt but your code is a mess. Specifically your brackets. You need to go back and check c++ syntax. Look at how the brackets are used for an if statement. You have random brackets that don't have any reason to be there. You have brackets that don't enclose any code. You're IF and IF ELSE statements brackets are commonly missing or don't enclose the code. I don't know Spanish so it difficult for me to try to figure out what you're trying to do but just by looking at your variables I tried to clean your code up. I did not check any logic or attempt to compile or run it.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>

using namespace std;

int main()
{

	float peso, tarifa;
	int anio, clase;


	cout << "Ingrese el anio del modelo de su vechiculo" << endl;
	cin >> anio;

	cout << "Ahora ingrese el peso de su vehiculo" << endl;
	cin >> peso;

	if (anio <= 1970)
	{

		if (peso < 2700)
		{
			clase = 1;
			tarifa = 16.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}

		else if ((peso >= 2700) && (peso <= 3800))
		{
			clase = 2;
			tarifa = 25.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}
		else if (peso > 3800)
		{
			clase = 3;
			tarifa = 46.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}
	}
	else if ((anio >= 1971) && (anio <= 1979))
	{

		if (peso < 2700)
		{
			clase = 4;
			tarifa = 27.00;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}

		else if ((peso >= 2700) && (peso <= 3800))
		{
			clase = 5;
			tarifa = 30.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}

		else if (peso > 3800)
		{
			clase = 6;
			tarifa = 52.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}

	}

	else if (anio >= 1980)
	{
		if (peso < 3500)
		{
			clase = 7;
			tarifa = 19.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}

		else if (peso >= 3500)
		{
			clase = 8;
			tarifa = 52.50;
			cout << "Su vehiculo de anio " << anio << " y peso " << peso << " es de clase " << clase << " con una tarifa de $" << tarifa << endl;
		}
	}

	return 0;
}
Hello mikeluna,

To many {} braces with some in the wrong place and it looks like there might be some missing. if/else statements are written this way:

1
2
3
4
5
6
7
8
9
10
if (condition)
{
    // Code here.
    // One or more lines.
}
else
{
    // Code here.
    // One or more lines.
}


This is a more acceptable way of writing an if/else statement. Notice how the {} braces line up.

Take line 18 of your code this opening brace is not needed, but if matched correctly will work.

This portion of you code for an example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        else if (anio>=1980)
                    {
                           {
                           if (peso<3500)
                           clase=7;
                           tarifa=19.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                           {
                           else if (peso>=3500)
                           clase=8;
                           tarifa=52.50;
                           cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
                           }
                    }


Lines 3 and 9 the opening brace needs to be placed after the if and else statements.

I will load up your code shortly and see if there is anything else I can find.

Hope that helps,

Andy
Last edited on
closed account (48T7M4Gy)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std;

int main()
{
    
    float peso, tarifa;
    int anio, clase;
    
    
    cout << "Ingrese el anio del modelo de su vechiculo" << endl;
    cin >> anio;
    
    cout << "Ahora ingrese el peso de su vehiculo"<<endl;
    cin >> peso;
    
    if (anio<=1970)
    {
        if (peso<2700)
        {
            clase=1;
            tarifa=16.50;
        }
        else if ((peso>=2700)&&(peso<=3800))
        {
            clase=2;
            tarifa=25.50;
        }
        else
        {
            clase=3;
            tarifa=46.50;
        }
    }
    else if ((anio>=1971)&&(anio<=1979))
    {
        if (peso<2700)
        {
            clase=4;
            tarifa=27.00;
        }
        else if ((peso>=2700)&&(peso<=3800))
        {
            clase=5;
            tarifa=30.50;
        }
        else
        {
            clase=6;
            tarifa=52.50;
        }
    }
    else
    {
        if (peso<3500)
        {
            clase=7;
            tarifa=19.50;
        }
        else
        {
            clase=8;
            tarifa=52.50;
        }
    }
    
    cout
    <<"Su vehiculo de anio "<<anio<<" y peso "<<peso
    <<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
    
    return 0;
}


Ingrese el anio del modelo de su vechiculo
1978
Ahora ingrese el peso de su vehiculo
890
Su vehiculo de anio 1978 y peso 890 es de clase 4 con una tarifa de $27
Program ended with exit code: 0
Last edited on
Use and editor or IDE that will do automatic indenting. That can really help you understand how the compiler will see the code. If the editor indents the line too many or too few spaces/tabs then you know you have a problem with your braces.
Topic archived. No new replies allowed.