There's nothing between these two statements to change the value of totalCharge
float totalCharge = 0.0;
if (totalCharge >= 16.67)
Printable View
Oh, heavens... :cry:
;) 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?
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...
Learn another profession
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.
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.