Im having a bit of a problem with this. Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
double water_bill(double usage_in_cubic_meters);
int main()
{
int usage_in_cubic_meters;
cout << "Enter the amount of usage in cubic meters: ";
cin >> usage_in_cubic_meters;
water_bill(usage_in_cubic_meters);
return 0;
}
double water_bill(double usage_in_cubic_meters)
{
double bill;
if (usage_in_cubic_meters <= 0)
cout << "ERROR: please enter a number above 0.";
else if (usage_in_cubic_meters <= 1000)
cout << "Your bill is: $15";
else if (usage_in_cubic_meters >= 1001 && <= 2000)
{
cout << "Your bill is: $";
bill = (usage_in_cubic_meters - 1000) * .018 + 15;
cout << bill;
}
else if (usage_in_cubic_meters >= 2001 && <= 3000)
{
cout << "Your bill is: $";
bill = ((usage_in_cubic_meters - 2000) * .02 + 15 + 18);
cout << bill;
}
else if (usage_in_cubic_meters >= 3001)
cout << "Your bill is: $75";
return 0;
}
Im having a problem with the else if statement >= 1001 && <= 2000. Same
with the one from 2001 to 3000. Apparently that is not a legal
statement because I'm getting syntax errors. Also I want to pass the
value of bill back to the main function without using any type of
pointer. How do I do that? return bill?
Marcus Kwok wrote:
Brian <br************@gmail.com> wrote: I see. How do I initialize a global variable? If I want my function to
pass a value back into main say for instance could I use a global
variable?
Please quote what you are responding to. If you are using Google to
read, then I think you have to click "Show Options" then click "Reply"
(don't click the "reply" at the bottom of the message).
You could use a global variable, but you shouldn't. Instead, you should
write your water_bill function so that you pass in the
usage_in_cubic_meters and return the amount of their bill. In main, you
can create a local variable in which to store the answer, and print the
message stating the amount to the user. Give this a try, and post your
code if you have any questions.
--
Marcus Kwok