Pages

Thursday, March 3, 2011

Nested if Sample program

Nested if statement

            Problem:
                        A General Service company hired you to write a program to compute the total charge for the services rendered to their client. The program will ask first if there is a material needed or not. If there is, the program will ask if what material is needed. The program will then ask for the cost of that materials entered and the number of hours of the services rendered. Finally, the program will compute for the labour cost and the total charge.
           
            Assumption:
                        Rate                             $45.00 per hour
                       
            Computation:
                        Labour             :           Rate * hours work
                        Total Charge   :           Labour + Cost of Materials

            Materials:
                       
1.)    Electricals
2.)    Electronics
3.)    Construction
4.)    Sanitary

            #include<iostream>
            using namespace std;

main( )
(
            string matname;
            int mat;
            float Rate = 45.00, Labour, cmat, tcharge, hours;
                        char k;
                        cout<<”Is there a material needed?  (y – yes, anykey for no) “;
                        cin>>k;
                                    if(k==’y’ || k==’Y’)
                                      {
                                                cout<<”1.) Electricals”<<endl
                                                       <<”2.) Electronics”<<endl
                                                       <<”3.) Construction”<<endl
                                                       <<”4.) Sanitation”<<endl
                                                       <<”Materials –> ”;
                                                cin>>mat;
                                               
                                                if(mat ==1)
                                                  {
                                                            matname = “Electricals”;
                                                   }
                                                else if(mat==2)
                                                  {
                                                            matname = “Electronics”;
                                                   }
                                                else if(mat==3)
                                                  {
                                                            matname = “Constructions”;
                                                   }
                                                else if(mat==4)
                                                  {
                                                            matname = “Sanitations”;
                                                   }

                                    cout<<endl<<”Enter the cost of “<<matname<<” material: $“;
                                    cin>>cmat;
                                    }
                                   
                                    else
                                                {
                                                cmat = 0;
                                                }

                        cout<<”Enter the number of hours of the services rendered: “;
                        cin>>hours;

                        Labour = Rate * hours;

                        tcharge = Labour + cmat;

                        cout<<”The total charge is: $“<<tcharge<<endl;

                        return 0;
                }
 
 
Sample Output:

            Is there a need for a material? (y – yes, anykey for no): y
1.)    Electricals
2.)    Electronics
3.)    Construction
4.)    Sanitations
Materials –> 2
           
            Enter the cost for Electronics material: $250
            Enter the number of hours worked: 4
               The total charge is: $430

2 comments:

  1. great sample problem for nested if.

    keep it up!

    ReplyDelete
  2. Thanks for posting this example, simple and pretty useful for nested if!

    ReplyDelete