what"s wrong with this problem? can anyone help..

// A PROGRAM FOR PASSENGER FARE, THE SYSTEM MUST ASK THE USER TO ENTER AGE OF PASSENGER.IF AGE IS BELOW 12,THERE IS A 10% DISCOUNT, ELSE 5% DISCOUNT
// THE USER WILL NOW ENTER THE DESTINATION CODE TO COMPUTE THE FARE.
// CODE FARE
// CHINA 2500
// JAPAN 3000

#include <iostream>
using namespace std;
int main()
{
int,age,dc1,dc2,china,japan;
double discount,discounted_fare;
dc1=china;
dc2=japan;
japan=3000;
china=2500;
cin>>age;
cin>>destination;
if (dc1)
else if (age>12)
discount=dc1*0.10;
else
discounted_fare=dc1-discount;
cout<<discounted_fare;
discount=dc1*0.05;
system ("pause");
return 0;
}





Before we do anything, lets post the code formatted correctly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    int,age,dc1,dc2,china,japan;
    double discount,discounted_fare;
    dc1=china;
    dc2=japan;
    japan=3000;
    china=2500;
    cin>>age;
    cin>>destination;
    if (dc1)
    else if (age>12)
        discount=dc1*0.10;
    else
        discounted_fare=dc1-discount;
    cout<<discounted_fare;
    discount=dc1*0.05;
    system ("pause");
    return 0;
}


You have an extra comma on line 5 after int
on line 12 you read into a value called destination which you never declared. From your description, destination should be a string..
Your logic after that needs to be fixed. You need to set the initial fare according to the value of the destination input by the user, which you haven't done. Then you need to write your conditional statements properly. I suggest you go look at the tutorial on conditionals if you don't understand how to use them.
Last edited on
ok sir thank you. now what do we do? i am not really expert in programming, but i did what i could..
Line 7 and 8: dc1 and dc2 are initialized to garbage values. Move the initialization of japan and china up.
Line 15: same problem because of line 7.
Line 17: discount is not initialized.
Line 19: discount initialized to garbage value because of line 7.
Last edited on
what do you mean by garbage values sir?
As for hints:

IF the destination is China, set the fare for China.
ELSE IF the destination is Japan, set the fare for Japan

and then...

IF the age is less than 12, calculate a 10% discount
ELSE calculate a 5% discount.

What you have is already quite close.
Say you declare a variable: int x;
If you try to use x without giving it a value first (initialization), the result is undefined. Usually the compiler just takes whatever value is in the memory. So variables need to be initialized before use.
Last edited on
You don't need dc1 and dc2. All you need is a single variable, let's call it fare, assigned to china or japan based on what the user inputs for destination.
ok thank you. i'll fix and try it later at home. i am at school today.. there is no dev-cpp software or program here. thanks again..
Last edited on
Topic archived. No new replies allowed.