|
-
August 19th, 2010, 04:43 PM
#16
Re: Will this code work?
 Originally Posted by ade161
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)
-
August 19th, 2010, 05:05 PM
#17
Re: Will this code work?
 Originally Posted by ProgrammerC++
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?
-
August 19th, 2010, 05:28 PM
#18
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...
-
August 19th, 2010, 11:05 PM
#19
Re: Will this code work?
 Originally Posted by TheGreatCthulhu
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?
-
August 19th, 2010, 11:37 PM
#20
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...
-
August 19th, 2010, 11:56 PM
#21
-
August 20th, 2010, 12:16 AM
#22
Re: Will this code work?
 Originally Posted by ProgrammerC++
Learn another profession
did you read my previous post above?
obviously not...
-
August 20th, 2010, 12:25 AM
#23
Re: Will this code work?
 Originally Posted by ade161
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.
-
August 20th, 2010, 05:50 AM
#24
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.
-
August 20th, 2010, 07:10 AM
#25
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|