electric bill

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shardul316
    New Member
    • Jan 2007
    • 20

    #1

    electric bill

    following program is compiled with zero errors but still i am getting wrong output
    can anybody find where i am wrong so as to get correct output
    [PHP]#include<iostre am>
    using namespace std;
    int main()
    {
    int units;
    char name[20];
    float cost,bill,total bill;
    cout<<"enter the name"<<endl;
    cin>>name;
    cout<<"read units"<<endl;
    cin>>units;
    if(units<=100)
    {
    cost=(units*60/100);
    bill=cost+50;
    }
    else if((units>100)& &(units<=200 ))
    {
    cost=(units*80/100);
    bill=cost+50;
    }
    else if(units>=300)
    {cost=(units*90/100);
    bill=50+cost;
    }
    if(bill>300)
    {
    totalbill=bill+ bill*(15/100);
    }
    else
    totalbill=bill;
    cout<<"MR."<<na me<<"YOUR BILL IS "<<totalbill<<" Rs only"<<endl;
    return 0;


    ] [/PHP]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by shardul316
    following program is compiled with zero errors but still i am getting wrong output
    can anybody find where i am wrong so as to get correct output
    It is hard to help you because you have not given the input plus the expected output and the erroneous output.

    However I note the following.

    A lot of you arithmetic is done as integer arithmetic, for instance

    cost=(units*60/100);

    units is a int as is 60 and 100, if units is not a multiple of 10, say 23 you might expect the answer 13.8 but because this is integer arithmetic you will get the answer 13. You need to do floating point arithmetic. This is easy, just use floating point constants instead of integer constants

    cost=(units*60. 0F/100.0F);

    A second error is if you examine the code you have no formula for cost if units >200 and <300.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      You have to be careful with these calculations:
      cost=(units*60/100);
      You will get integer division and end with zero.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by willakawill
        You will get integer division and end with zero.
        May end with 0 if units == 1 or 0 or -1

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by Banfa
          It is hard to help you because you have not given the input plus the expected output and the erroneous output.

          However I note the following.

          A lot of you arithmetic is done as integer arithmetic, for instance

          cost=(units*60/100);

          units is a int as is 60 and 100, if units is not a multiple of 10, say 23 you might expect the answer 13.8 but because this is integer arithmetic you will get the answer 13. You need to do floating point arithmetic. This is easy, just use floating point constants instead of integer constants

          cost=(units*60. 0F/100.0F);

          A second error is if you examine the code you have no formula for cost if units >200 and <300.
          Either there is something wrong with my browser or you have used admin status to bump your reply above mine. This post was not visible to me until now.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by willakawill
            Either there is something wrong with my browser or you have used admin status to bump your reply above mine. This post was not visible to me until now.
            Well I didn't no nothing, hmmm keep an eye out and let me know if this happens again.

            My broswer often fails to display the text of posts properly but always leaves page space for them.

            Comment

            Working...