|
-
August 18th, 2010, 10:43 PM
#1
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.
-
August 18th, 2010, 11:09 PM
#2
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
-
August 18th, 2010, 11:25 PM
#3
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.
-
August 18th, 2010, 11:46 PM
#4
Re: Will this code work?
use floats instead of ints
-
August 19th, 2010, 12:32 PM
#5
Re: Will this code work?
ade161, out of curiosity has your instructor taught how to step through and debug a program?
-
August 19th, 2010, 12:35 PM
#6
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)
-
August 19th, 2010, 01:54 PM
#7
Re: Will this code work?
 Originally Posted by Arjay
ade161, out of curiosity has your instructor taught how to step through and debug a program?
not yet.
i am studying ahead.
-
August 19th, 2010, 01:55 PM
#8
Re: Will this code work?
 Originally Posted by gcdef
what do you think the value of totalcharge is when you execute this line of code?
If (totalcharge <= 16.67)
16.67
-
August 19th, 2010, 02:10 PM
#9
Re: Will this code work?
it is *\(0.0)/*
-
August 19th, 2010, 02:12 PM
#10
Re: Will this code work?
 Originally Posted by ade161
16.67
Why would you think that? And if you already know its value at that point, why use an if statement?
-
August 19th, 2010, 03:19 PM
#11
Re: Will this code work?
 Originally Posted by GCDEF
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.
-
August 19th, 2010, 03:19 PM
#12
Re: Will this code work?
 Originally Posted by TheComputer
 it is *\(0.0)/*
what are you indicating?
-
August 19th, 2010, 03:53 PM
#13
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
-
August 19th, 2010, 04:17 PM
#14
Re: Will this code work?
 Originally Posted by Pocoya
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.
-
August 19th, 2010, 04:41 PM
#15
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
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
|