I had to make a program wherein I was supposed to take age
and experience
(whether the employee is experienced or not) of an employee as input from the user and print out his/her salary. The salary was subject to the following conditions:
- If employee is inexperienced, salary=2000 irrespective of age.
- For an experienced employee:
- If age<=28, salary=3000.
- If 28<age<=35, salary=4800.
- If age>35, salary=6000.
I made the following C++ program:
.
.
.
cout<<"The salary of the employee is Rs."<<(experience?sal1:2000); //LINE1
sal1=((age<=28)?3000:sal2);
sal2=((age<=35)?4800:6000);
return 0;
}
where age
, sal1
, sal2
are declared as int
and experience
as bool
.
experience=1
is entered by user for experienced employee and otherwise experience=0
.
But whenever experience==1
and any age>28
is entered, I get unexpectedly large integral results whereas the code produces absolutely perfect results when conditional operator is nested.(i.e I copy the expression of sal1
to the truth expression in LINE1 and copy the expression of sal2
into expression of sal1
)
Please explain what is the difference between both of these codes and why am I getting unexpected results in the first case.
NOTE: I have used the gcc g++ compiler for compilation of my code. Please tell if it's the fault of the compiler, the operator or is there any other issue.