CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Will this code work?

    Quote Originally Posted by ade161 View Post
    how does this look?

    //ch5 - 13.cpp : calculates and displays number of gallons & total charge for the water
    //created/revised by <> on <8-18-10>

    #include <iostream>
    using namespace std;

    int main()
    {
    //declare variables
    float previousReadings = 0.0;
    float currentReadings = 0.0;
    float gallonsUsed = 0.0;
    float totalCharge = 0.0;

    //enter input items
    cout <<"Enter the current readings:";
    cin >> currentReadings;
    cout <<"Enter the previous readings:";
    cin >> previousReadings;

    //calculates the gallons used
    gallonsUsed = currentReadings - previousReadings;

    //calculates the total charge
    if (totalCharge >= 16.67)
    {
    totalCharge = (gallonsUsed / 1000) *7;
    cout <<"totalCharge:" << totalCharge << endl;
    }
    else
    {
    totalCharge = 16.67;
    cout <<"total charge:" << totalCharge << endl;
    }
    //end if

    cout <<"gallons used:" << gallonsUsed << endl;
    cout <<"total charge:" << totalCharge << endl;

    return 0;
    } //end of main function
    There's nothing between these two statements to change the value of totalCharge

    float totalCharge = 0.0;
    if (totalCharge >= 16.67)

  2. #17
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by ProgrammerC++ View Post
    Code:
    //calculates the gallons used
    gallonsUsed = currentReadings - previousReadings;
    totalCharge = (gallonsUsed / 1000.0f) * 7.0f;
    
    //calculates the total charge
    if (totalCharge < 16.67f)
    {
    totalCharge = 16.67f;
    }
    
    cout <<"total charge:" << totalCharge << endl;
    //end if
    1. why would you calculate the total charge along with the gallons used in the same statement? To me it should be included in the if statement as a false path wouldnt you agree?

  3. #18
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Will this code work?

    Oh, heavens...

    The last few post have been very straightforward, please try to concentrate.

    Imagine you're the CPU executing the code, line by line. This is a part of your original program:
    Code:
    // You're interested in totalCharge, ignore other stuff.
    int main()
    {
    //declare variables
    float previousReadings = 0.0;
    float currentReadings = 0.0;
    float gallonsUsed = 0.0;
    float totalCharge = 0.0;    // OK. Now totalCharge = 0.0 [remember the value]
    
    //enter input items
    cout <<"Enter the current readings:";    // totalCharge hasn't changed [still 0.0]
    cin >> currentReadings;                     // totalCharge hasn't changed [still 0.0]
    cout <<"Enter the previous readings:";   // totalCharge hasn't changed [still 0.0]
    cin >> previousReadings;                   // totalCharge hasn't changed [still 0.0]
    
    //calculates the gallons used
    gallonsUsed = currentReadings - previousReadings;   // totalCharge hasn't changed [still 0.0]
    
    //calculates the total charge -- BUT totalCharge hasn't changed [still 0.0]
    if (totalCharge >= 16.67)   // totalCharge hasn't changed [still 0.0], so...
    {
        // will this execute?
        totalCharge = (gallonsUsed / 1000) *7;
        cout <<"totalCharge:" << totalCharge << endl;
    }
    // etc...

  4. #19
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by TheGreatCthulhu View Post
    Oh, heavens...

    The last few post have been very straightforward, please try to concentrate.

    Imagine you're the CPU executing the code, line by line. This is a part of your original program:
    Code:
    // You're interested in totalCharge, ignore other stuff.
    int main()
    {
    //declare variables
    float previousReadings = 0.0;
    float currentReadings = 0.0;
    float gallonsUsed = 0.0;
    float totalCharge = 0.0;    // OK. Now totalCharge = 0.0 [remember the value]
    
    //enter input items
    cout <<"Enter the current readings:";    // totalCharge hasn't changed [still 0.0]
    cin >> currentReadings;                     // totalCharge hasn't changed [still 0.0]
    cout <<"Enter the previous readings:";   // totalCharge hasn't changed [still 0.0]
    cin >> previousReadings;                   // totalCharge hasn't changed [still 0.0]
    
    //calculates the gallons used
    gallonsUsed = currentReadings - previousReadings;   // totalCharge hasn't changed [still 0.0]
    
    //calculates the total charge -- BUT totalCharge hasn't changed [still 0.0]
    if (totalCharge >= 16.67)   // totalCharge hasn't changed [still 0.0], so...
    {
        // will this execute?
        totalCharge = (gallonsUsed / 1000) *7;
        cout <<"totalCharge:" << totalCharge << endl;
    }
    // etc...
    so here is the corrected version

    #include <iostream>
    using namespace std;

    int main()
    {
    //declare variables
    float previousReadings = 0.0;
    float currentReadings = 0.0;
    float gallonsUsed = 0.0;
    float totalCharge = 0.0;

    //enter input items
    cout <<"Enter the current readings:";
    cin >> currentReadings;
    cout <<"Enter the previous readings:";
    cin >> previousReadings;

    //calculates the gallons used and total charged
    gallonsUsed = currentReadings - previousReadings;
    totalCharge = (gallonsUsed / 1000) *7;

    //calculates the total charge
    if (totalCharge < 16.67)
    {
    totalCharge = 16.67;
    cout <<"total charge:" << totalCharge << endl;
    }
    //end if

    cout <<"gallons used:" << gallonsUsed << endl;
    cout <<"total charge:" << totalCharge << endl;

    return 0;
    } //end of main function





    does it make sense?

  5. #20
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    attention everyone:
    i debugged the program (i used a set of sample data) and the results are as shown. Is this correct?


    Enter the current readings:16000
    Enter the previous readings: 13000
    gallons used:3000
    total charge:21
    Press any key to continue...

  6. #21
    Join Date
    Jul 2009
    Posts
    154

    Re: Will this code work?

    Learn another profession

  7. #22
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by ProgrammerC++ View Post
    Learn another profession
    did you read my previous post above?

    obviously not...

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Will this code work?

    Quote Originally Posted by ade161 View Post
    did you read my previous post above?

    obviously not...
    I think he is suggesting that you check your own work.

    In other words, there's no need to ask a programming forum to check the math for you.

  9. #24
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Will this code work?

    Looks like you are just starting out, and at least you're making an effort with this.

    When you test something, you want to go through the main paths, and also try some boundary conditions, or input that is just on the boundary between two cases.

    For this program, you want to at least try input that gives you a charge that would be less than 16.67, exactly 16.67, and more than 16.67. Ideally you would try extreme cases, e.g. what happens for negative numbers, invalid numbers, very large numbers etc.

    Looking at the code you posted, I think you have at least a minor bug still to find.

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Will this code work?

    Stylistically, you should keep code as simple as possible.
    Why write totalCharge = (gallonsUsed / 1000) *7 when you could write totalCharge = gallonsUsed *.007?

    Also, the requirement is a little vague to me. You may very well be interpreting it correctly, but $7 per 1000 gallons could also mean that the charge is always a multiple of $7. If I were given that assignment, I'd ask for clarification.

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured