If else statement problem and solve


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: If else statement problem and solve

  1. #1
    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. #2
    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. #3
    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


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

  4. #4
    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. #5
    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.

Similar Threads

  1. How to solve this problem??
    By LuisFigo in forum Database
    Replies: 3
    Last Post: 10-08-2005, 10:21 PM
  2. Replies: 4
    Last Post: 03-04-2005, 01:55 PM
  3. solve this problem urgently
    By manish jaiswal in forum ASP.NET
    Replies: 2
    Last Post: 05-08-2003, 04:14 PM
  4. Plz solve this problem
    By mohanreddy in forum Java
    Replies: 0
    Last Post: 07-19-2001, 11:03 PM
  5. Using SQLDMO to solve security problem...
    By Javier Vargas in forum Database
    Replies: 2
    Last Post: 07-13-2001, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center