If else statement problem and solve

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: If else statement problem and solve

1. Registered User
Join Date
Oct 2005
Posts
32

## If else statement problem and solve

Problem :
The " I can't believe it's true " movie company is looking for extras to be in their new movie.your job is to write a program that calculates the total pay due to an extra based on the number of hours they are needed on the set. the pay is calculated using the fallowing rules. there is base rate of \$1000 plus an hourly rate of \$20 for someone working less that 8 hours, an hourly rate \$25 dollars for someone acting from 8 to 15 hours,an hourly rate of \$37 for someone working 16 to 30 hours and \$ 45 an hour for those working for more than 30 hours.use the conditional operator
ask the user to input the number of hours they have worked.
write the output in the following format:
you have worked X hours and your total pay is \$ yyy.yy

Solve:
Code:
#include<iostream>

int main ()
{
float totalpay;
int hours;
cout<<"Enter the number of hours you worked:";
cin>>hours;

if(hours < 8)
totalpay=hours*20+1000;
else if (hours >= 8 &&  hours <= 15)
totalpay=hours*25+1000;
else if (hours >=16 &&  hours <= 30)
totalpay=hours*37+1000;
else if  (hours > 30)
totalpay=hours*45+1000;

cout<<"\nYou have worked  "<<hours<<"  hours and your total pay is "<<totalpay<<" dollars"<<endl;
cin >> hours;
return 0 ;

}
Mansur

2. Registered User
Join Date
May 2005
Posts
29
thank you Mansur
but the Q said that useing the conditional operator
is it the same as I send it to you ?
Code:
#include<iostream>
using namespace std;

int main ()
{ float totalpay;
int hours;
cout<<"Enter the number of hours you worked:";
cin>>hours;
totalpay=(hours<=8)?hours*20=1000:
(hours<=15)?hours*25+1000:
Hours*45+1000;
cout<<"\nYou have worked"<<hours<<"hours and your total pay is "<<totalpay<<"dollars"<<endl;
cin>>hours;

return 0 ;
}
thank you agin :)
Last edited by malehda3y; 10-18-2005 at 12:44 AM.

3. Registered User
Join Date
Oct 2005
Posts
32
You sure ?
The ? alternative :
the ? operator can be used to replace if/else statements of general form :
if ( condition) expression;
else expression;
the ? is called a ternary operator and takes the general form
Exp1 ? Exp2 : Exp3

The complete Reference
Borland C++
http://www.osborne.com
I will try to solve in ternary operator
Mansur

4. Registered User
Join Date
May 2005
Posts
29
it 's ok Mansur dont warry about it :)
dont do anything with it
you really helped me alot and Thank you alot :D

but would you main if you take alook at the other probelm i send it to you please ??
Thank you so muchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
alllah ya7fe9*eak :)

5. Senior Member
Join Date
Dec 2003
Posts
3,366
The ? operator is ugly and there is no good reason to use it. It makes the code harder to read and should be avoided in real code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise