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
#include <iostream>
usingnamespace 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;
}{
elseif ((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;
}
{
elseif (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;
}
}
elseif ((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;
}
{
elseif ((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;
}
{
elseif (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;
}
}
elseif (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;
}
{
elseif (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.
#include <iostream>
usingnamespace 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;
}
elseif ((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;
}
elseif (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;
}
}
elseif ((anio>=1971)&&(anio<=1979))
{
// etc.
}
elseif (anio>=1980)
{
// etc.
}
return 0;
}
Actually, since all the cout statements are the same, you only need one, at the end:
#include <iostream>
usingnamespace 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;
}
elseif ((peso>=2700)&&(peso<=3800))
{
clase=2;
tarifa=25.50;
}
elseif (peso>3800)
{
clase=3;
tarifa=46.50;
}
}
elseif ((anio>=1971)&&(anio<=1979))
{
// etc.
}
elseif (anio>=1980)
{
// etc.
}
cout<<"Su vehiculo de anio "<<anio<<" y peso "<<peso<<" es de clase "<<clase<<" con una tarifa de $"<<tarifa<<endl;
return 0;
}
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.
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
elseif (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;
}
{
elseif (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.
#include <iostream>
usingnamespace 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;
}
elseif ((peso>=2700)&&(peso<=3800))
{
clase=2;
tarifa=25.50;
}
else
{
clase=3;
tarifa=46.50;
}
}
elseif ((anio>=1971)&&(anio<=1979))
{
if (peso<2700)
{
clase=4;
tarifa=27.00;
}
elseif ((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
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.