CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: C++ Problem Help

1. Junior Member
Join Date
Sep 2012
Posts
5

## C++ Problem Help

I am trying to solve the following problem.

Here is the problem specification:
Allenton Water Department wants a program that calculates a customer's monthly water bill. The clerk will enter the current and the 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 is \$16.67. (In other words, every customer must pay at least \$16.67).

In one of the questions, I am asked to desk-check my algorithm with 13000 and 16000 as the previous and the current meter readings respectively. The second desk-check use 1650 and 3675.

I have a couple of questions concerning the problem.

1. With the first desk check I got 3000 for the gallons used, and \$21 for the total charge. For the second desk check I got 2025 for the gallons used, and \$14.175; however, since the minimum charge is \$16.67, I wrote down \$16.67 in my desk-check table, since \$14.175 < \$16.67. I want to know if my answers are right.

2. In my IPO chart, for my fourth step, I have the following pseudocodes:
if (total charge < 16.67)
total charge = 16.67
end if

I would like to know if the pseudocode is correct.

3. For the coding solution, I essentially have two different versions, and for some reason, both versions give the same results for the desk-check. I would like to know which of the two versions are correct. I am also asked to display the total charge in fixed-point notation with two decimal places.

First coding solution:

//Exercise13.cpp - displays the gallons used and the total charge
//Created/revised by <Patrick> on <September 29>
Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declare variables and named constants
double previousMeterReadings = 0.0;
double currentMeterReadings = 0.0;
double gallonsWaterUsed = 0.0;
double totalCharge = 0.0;
const double CHARGE_PER_GALLON = 0.007;

//enter input items
cout << "Enter the previous readings: ";
cout << "Enter the current readings: ";

//calculate gallons of water used and total charge
totalCharge = CHARGE_PER_GALLON * gallonsWaterUsed;

//calculate total charge
if (totalCharge < 16.67)
totalCharge = 16.67;
//end if

cout << "Gallons used: " << gallonsWaterUsed << endl;
cout << fixed << setprecision(2);
cout << "Total charge: \$" << totalCharge << endl;

system("pause");
return 0;
}	//end of main function```
Second coding solution:

//Exercise13.cpp - displays the gallons used and the total charge
//Created/revised by <Patrick> on <September 29>
Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declare variables and named constants
double previousReadings = 0.0;
double currentReadings = 0.0;
double gallonsUsed = 0.0;
double totalCharge = 0.0;
const double CHARGE_PER_GALLON = 0.007;

//enter input items
cout << "Enter the previous readings: ";
cout << "Enter the current readings: ";

//calculate gallons used and total charge
totalCharge = CHARGE_PER_GALLON * gallonsUsed;

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

cout << "Gallons used: " << gallonsUsed << endl;
cout << fixed << setprecision(2);
cout << "Total charge: \$" << totalCharge << endl;

system("pause");
return 0;
}	//end of main function```
Last edited by cilu; October 2nd, 2012 at 04:37 AM. Reason: code tags

2. ## Re: C++ Problem Help

1. Yes, that seems correct.

2. Yes, in pseudo code. Notice that comparing doubles is troublesome, because doubles don't have a precise binary representation. Doubles should be compared not directly, but you should rather compare that the difference between them is smaller than a given, small value. Read more here: http://forums.codeguru.com/showthrea...-representated.

3. Both are corrected in computing the cost, but the second would incorrectly display the cost twice, when the cost is smaller than the specified minimum.

3. Junior Member
Join Date
Sep 2012
Posts
5

## Re: C++ Problem Help

Thank you for your help.

#### 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