Where do i put the curly braces in this else if statement?

#include <iostream>
#include <string>
using namespace std;
int main()
{
int age,china_fare,japan_fare;
double discount,discounted_fare;
char destination;
japan_fare=3000;
china_fare=2500;
cin>>age;
cin>>destination;
if (destination==japan_fare && age>12)
discount=japan_fare*0.10;
discounted_fare==japan_fare-discount;
else if (destination==japan_fare && age<12)
discount=japan_fare*0.05;
discounted_fare=japan_fare-discount;
else if (destination==china_fare && age>12)
discount=china_fare*0.10;
discounted_fare=china_fare-discount;
else if (destination==china_fare && age<12)
discount=china_fare*0.05;
discounted_fare=china_fare-discount;
cout<<age<<discount<<destination;
system ("pause");
return 0;


can someone show me an example.
if statements check for the condition in the ( )'s is true. If the condition is true it either looks for the next line and executes that, or it looks for { }'s and executes everything inside the braces.

For example:

1
2
3
if(true)
    //do this one line of code
//and then move on to whatever is next 

or
1
2
3
4
5
if(true)
{
/* Do everything that is inside of these 
   curly braces before moving on. */
}



The same holds true for all of the loops (while, for, etc.)

EDIT: looking at your code I've spotted a couple problems.
1
2
3
if (destination==japan_fare && age>12)
discount=japan_fare*0.10;
discounted_fare==japan_fare-discount;//this statement does not make sense 


"==" is a comparison operator. It checks to see if the value on the left is the same as the value on the right. If the values match it returns True, if not it returns False. That is why you need to use the "==" in if statements, because an if statement is checking to see if something is true or false.

Just a single "=" is an assignment operator. It takes the value on the right and assigns that value to the variable on the left (when logical to do so. cannot assign a double to a string etc.).
Last edited on
Topic archived. No new replies allowed.