CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Aug 2010
    Posts
    51

    Will this code work?

    here is the problem:

    Allenton Water Department wants a program that calculates a customers monthly water bill. The clerk will enter the current and previous meter readings. The program should calculate and display the number of gallons of water used and the total charge for the water. The charge for water is $7 per 1000 gallons. However, there is a minimum charge of $16.67. ( in other words, every customer must pay at least $16.67)


    does this code make sense?


    //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.0) *7.0;
    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
    Last edited by ade161; August 19th, 2010 at 05:42 PM.

  2. #2
    Join Date
    Jul 2009
    Posts
    154

    Re: Will this code work?

    you forgot { } brackets in your if but it should compile once you fixed that.

    If it does what your question was I dont know I didnt read that I'm not going to do your homework the forum policy even states we dont do your homework

    Edit: Oh and there's some other stuff wrong too but rather than telling you what it is I prefer to spend my time saying that there's something more wrong with your code and that I prefer to say this

  3. #3
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    thanks so far. could i get a hint as to what else would be wrong? like is it the if statement, or does if have to do with my logic when trying to solve the problem?


    by the way, im not asking you or anyone to do my homework that is not the reason i started this thread.

  4. #4
    Join Date
    Jul 2009
    Posts
    154

    Re: Will this code work?

    use floats instead of ints

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

    Re: Will this code work?

    ade161, out of curiosity has your instructor taught how to step through and debug a program?

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

    Re: Will this code work?

    What do you think the value of totalCharge is when you execute this line of code?


    if (totalCharge <= 16.67)

  7. #7
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by Arjay View Post
    ade161, out of curiosity has your instructor taught how to step through and debug a program?
    not yet.

    i am studying ahead.

  8. #8
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by gcdef View Post
    what do you think the value of totalcharge is when you execute this line of code?


    If (totalcharge <= 16.67)
    16.67

  9. #9
    Join Date
    Jun 2010
    Posts
    50

    Re: Will this code work?

    it is *\(0.0)/*

  10. #10
    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
    16.67
    Why would you think that? And if you already know its value at that point, why use an if statement?

  11. #11
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by GCDEF View Post
    Why would you think that? And if you already know its value at that point, why use an if statement?
    because they have to be charged 16.67 no matter what.
    Last edited by ade161; August 19th, 2010 at 03:47 PM.

  12. #12
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by TheComputer View Post
    it is *\(0.0)/*
    what are you indicating?

  13. #13
    Join Date
    Jul 2010
    Posts
    31

    Re: Will this code work?

    Hello, ade buddy

    As the minimal totalcharge is 16.67, formula to calculate totalcharge should come first before the conditional check against the minimum value. Other than that, your program looks fine to me

  14. #14
    Join Date
    Aug 2010
    Posts
    51

    Re: Will this code work?

    Quote Originally Posted by Pocoya View Post
    Hello, ade buddy

    As the minimal totalcharge is 16.67, formula to calculate totalcharge should come first before the conditional check against the minimum value. Other than that, your program looks fine to me
    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.0) *7.0;
    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
    Last edited by ade161; August 19th, 2010 at 05:42 PM.

  15. #15
    Join Date
    Jul 2009
    Posts
    154

    Re: Will this code work?

    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

Page 1 of 2 12 LastLast

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