IF statement problem
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
|
#include <iostream.h>
#include <conio.h>
main()
{
int airline, destination, category;
cout <<"Please choose your preferred airline. ";
cout <<"\n1. Airline1.\n2. Airline2"<<endl;
cin >> airline;
if (airline < 1 || airline > 2)
{
cout <<"\nInvalid input.";
}
else
{
if (airline == 1)
cout << "\nYou have chosen Airline1."<<endl;
cout << "\nPlease choose your preferred destination. ";
cout << "\n1. A - B\n2. B - C"<<endl;
cin >> destination;
{
if (destination == 1)
cout << "\nPlease choose the following category.";
cout << "\n1.Children\n2.Adults"<<endl;
cin >> category;
switch (category)
{
case 1:
cout << "\nFares: 200.00\nAirport Tax: 5%";
break;
case 2:
cout << "\nFares: 257.00\nAirport Tax: 5%";
break;
default:
cout << "\nInvalid input.";
}
else
if (destination == 2)
cout << "\nPlease choose the following category."<<endl;
cout << "\n1.Children\n2.Adults"<<endl;
cin >> category;
switch (category)
{
case 1:
cout << "\nFares: 195.00\nAirport Tax: 5%";
break;
case 2:
cout << "\nFares: 245.00\nAirport Tax: 5%";
break;
default:
cout << "\nInvalid input.";
} }
}
getch();
}
|
I got an
else misplaced error. Can someone help me out with this. It makes me frustrated. Any help would be appreciated.
- cplusx2
You didn't wrap lines 18-37 in {}, so only line 24 is inside the if.
Sorry dude, but i don't understand ya. Care to explain?
Btw, i wrapped the line at 18 but still the error keep appearing.
Maybe if the code is consistently indented it will be clearer:
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
|
#include <iostream.h>
#include <conio.h>
main()
{
int airline, destination, category;
cout <<"Please choose your preferred airline. ";
cout <<"\n1. Airline1.\n2. Airline2"<<endl;
cin >> airline;
if (airline < 1 || airline > 2)
{
cout <<"\nInvalid input.";
}
else
{
if (airline == 1) // where is the brace { ?? ========
cout << "\nYou have chosen Airline1."<<endl;
cout << "\nPlease choose your preferred destination. ";
cout << "\n1. A - B\n2. B - C"<<endl;
cin >> destination;
{ // what is this doing here ?? ========
if (destination == 1) // where is the brace { ?? ========
cout << "\nPlease choose the following category.";
cout << "\n1.Children\n2.Adults"<<endl;
cin >> category;
switch (category)
{
case 1:
cout << "\nFares: 200.00\nAirport Tax: 5%";
break;
case 2:
cout << "\nFares: 257.00\nAirport Tax: 5%";
break;
default:
cout << "\nInvalid input.";
}
else
if (destination == 2)
cout << "\nPlease choose the following category."<<endl;
cout << "\n1.Children\n2.Adults"<<endl;
cin >> category;
switch (category)
{
case 1:
cout << "\nFares: 195.00\nAirport Tax: 5%";
break;
case 2:
cout << "\nFares: 245.00\nAirport Tax: 5%";
break;
default:
cout << "\nInvalid input.";
}
}
}
getch();
}
|
Ahh i see the whole pic now, literally. Anyway, thx to everyone, especially Galik. Dude, u have my respect! XD. Thanx again.
- cplusx2
Topic archived. No new replies allowed.