Need help -- starting functions

B

Brian

#include <iostream>
#include <cmath>
using namespace std;


int water_bill(int usage_in_cubic_meters);

int main()
{
cout << "Enter the amount of usage in cubic meters: ";
return 0;
}

int water_bill(int usage_in_cubic_meters)
{

cin >> usage_in_cubic_meters;

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 > 3000)
cout << "Your bill is $70";

return usage_in_cubic_meters;

}

how come this wont go into the water_bill function and it just keeps
stopping after the main function?
 
A

Augusto KhaoticMind da Silva

i think you are used to work with script languages, or comething.
in C++ ALL the code that is executed is what is inside the main()
function (and the functions that it calls).
So, if you want the water_bill code to run you need to put a call to it
inside the main. The code bellow should be what you are looking for.

int main()
{
cout << "Enter the amount of usage in cubic meters: ";
int usage_in_cubic_meters;
cin >> usage_in_cubic_meters;
water_bill(usage_in_cubic_meters);
return 0;
}
 
V

Victor Bazarov

Brian said:
#include <iostream>
#include <cmath>
using namespace std;


int water_bill(int usage_in_cubic_meters);

int main()
{
cout << "Enter the amount of usage in cubic meters: ";
return 0;
}

int water_bill(int usage_in_cubic_meters)
{

cin >> usage_in_cubic_meters;

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 > 3000)
cout << "Your bill is $70";

return usage_in_cubic_meters;

}

how come this wont go into the water_bill function and it just keeps
stopping after the main function?

Functions need to be _called_. You need to add something like

cout << water_bill(0) << endl;

before the 'return' in 'main'.

V
 
H

Howard

in C++ ALL the code that is executed is what is inside the main()
function (and the functions that it calls).

I don't want to add to Brian's confusion, but that's not actually ALL the
code that gets executed. Global and static objects get initialized, prior
to the execution of main, and that initialization may require calling
constructors (and associated code). Also, the system probably executes some
startup code (which is not part of the code you write) when executing an
app.

But obviously, the main point was that if he wants a function to be called,
then he has to call it (from main, in this simple example).

-Howard
 
B

Brian

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?
 
B

Brian

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?
 
M

Marcus Kwok

Brian said:
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.
 
N

Niklas Norrthon

Brian said:
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?

First: Don't use global variables. At least not until you have a year
or two of C++ experience.

Second: You initialize globals in the same way you initialize local
varibles:

int global = 4711; /* This variable is initialized before main
* is called by the OS. Perhaps it's even
* compiled into the program
*/

int main()
{
int local = 9;
return 0;
}

Third: Globals are not used to pass values back to main. Use return
values, or reference arguments (or occationly pointer arguments).

int unlucky()
{
return 13;
}

void meaning_of_life(int& x)
{
x = 42;
}

int main()
{
int x = unlucky();
int z;
meaning_of_life(z);
// z is now 42
return 0; // Means success, passed to the OS.
}

/Niklas Norrthon
 
B

Brian

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?
 
O

osmium

Brian said:
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)

else if(usage >= 1001 && uasge <= 2000)

That's a pretty common mistake. A person senses an implicit operand, the
computer doesn't.
 
H

Howard

Brian said:
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;

Ok, you declare bill here. But you need to set it to some value in all
cases below. This function should only _compute_ the bill, not display the
results (nor ask the user to re-enter data).
if (usage_in_cubic_meters <= 0)
cout << "ERROR: please enter a number above 0.";

bill = -1.0;

? If you need an "illegal value", this might work. There are other methods,
but this is one easy way. But really, you should detect and handle invalid
input before ever calling this function.
else if (usage_in_cubic_meters <= 1000)
cout << "Your bill is: $15";

bill = 15.0;
else if (usage_in_cubic_meters >= 1001 && <= 2000)

This is not legal syntax. I take it you mean:

else if ((usage_in_cubic_meters >= 1001) && (usage_in_cubic_meters <= 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";

bill = 75.0;
return 0;

return bill;

But first, the statements above need to always assign a value to bill
(instead of outputting information to the user).
}

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?

See notes above.

-Howard
 
M

Marcus Kwok

Brian said:
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;

Really, you should be doing your error checking here instead of inside
water_bill.
water_bill(usage_in_cubic_meters);

Since water_bill returns a double, you should have water_bill just
compute the amount of the bill, and store it in a variable. Like:

double bill = water_bill(usage_in_cubic_meters);

This will declare a variable called "bill" inside the main function.
This bill is different from the bill in the water_bill function.

Then, now that you have the amount of the water bill stored in the
variable "bill" in main, you can use this in your output statement.
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";

See notes above regarding output statements.
else if (usage_in_cubic_meters >= 1001 && <= 2000)

Should be:

else if (usage_in_cubic_meters >= 1001 && usage_in_cubic_meters <= 2000)
{
cout << "Your bill is: $";
bill = (usage_in_cubic_meters - 1000) * .018 + 15;
cout << bill;
}

else if (usage_in_cubic_meters >= 2001 && <= 3000)

See above.
{
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;

return bill;
}

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?

Yes, see above. Your water_bill function should only compute the value
of the bill and return it. In main, you will "capture" the value (via
the return statement) into a variable, which you can use for output.
 
B

Brian

Marcus said:
Yes, see above. Your water_bill function should only compute the value
of the bill and return it. In main, you will "capture" the value (via
the return statement) into a variable, which you can use for output.

So I finished the program and this is what I came up with. It seems to
work well. Thanks for your help.

#include <iostream>
#include <iomanip>
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);
double bill = water_bill(usage_in_cubic_meters);
if (bill == 0)
{
cout << "ERROR: Please enter a number above 0";
cout << endl;
}

else
{
cout << "Amount of water used";
cout << " Bill Total";
cout << endl;
cout << "---------------------";
cout << " ----------";
cout << endl;
cout << usage_in_cubic_meters;
cout << " Cubic Meters";
cout << " ";
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << bill;
cout << endl;
}
return 0;
}

double water_bill(double usage_in_cubic_meters)
{
double bill;

if (usage_in_cubic_meters <= 0)
bill = 0;

else if (usage_in_cubic_meters <= 1000)
bill = 15;

else if((usage_in_cubic_meters >= 1001) && (usage_in_cubic_meters <=
2000))
bill = (usage_in_cubic_meters - 1000) * .018 + 15;

else if ((usage_in_cubic_meters >= 2001) && (usage_in_cubic_meters <=
3000))
bill = ((usage_in_cubic_meters - 2000) * .02 + 15 + 18);

else if (usage_in_cubic_meters >= 3001)
bill = 70;


return bill;
}
 
I

int2str

Brian said:
So I finished the program and this is what I came up with. It seems to
work well. Thanks for your help.

Not quite...
Try this in your main program:

cout << water_bill( 3000.5 ) << endl;

See the rest of my comments below for details.
#include <iostream>
#include <iomanip>
using namespace std;


double water_bill(double usage_in_cubic_meters);

int main()
{
int usage_in_cubic_meters;

ALWAYS initialize your variables...
cout << "Enter the amount of usage in cubic meters: ";
cin >> usage_in_cubic_meters;

You are not validating the input here.
water_bill(usage_in_cubic_meters);
double bill = water_bill(usage_in_cubic_meters);

Why are you calling water_bill twice here?
Only the second line with the assignment really matters.
if (bill == 0)
{
cout << "ERROR: Please enter a number above 0";
cout << endl;
}

else
{
cout << "Amount of water used";
cout << " Bill Total";
cout << endl;
cout << "---------------------";
cout << " ----------";
cout << endl;

You can use tabs ("\t") or the setw() io manipulator to achieve nicer
output. Also, you are allowed to do more than one operator << at a
time.
cout << usage_in_cubic_meters;
cout << " Cubic Meters";
cout << " ";
cout << fixed;
cout << showpoint;
cout << setprecision(2);
cout << bill;

Like this:
cout << fixed << showpoint << setprecision(2) << bill << endl;
cout << endl;
}
return 0;

return 0 is optional in main().
}

double water_bill(double usage_in_cubic_meters)
{
double bill;

ALWAYS initialize your variables.
if (usage_in_cubic_meters <= 0)
bill = 0;

else if (usage_in_cubic_meters <= 1000)
bill = 15;

Here you cover usabe UP TO and INCLUDING 1000....
else if((usage_in_cubic_meters >= 1001) && (usage_in_cubic_meters <=
2000))

.... but the next range starts at 1001! Since usage_in_cubic_meters is a
double, 1000.5 is a possible input value that would not match any of
your if statements and cause the uninitialized (!) variable bill to be
returned. <-- That's a BUG

Just change these to usage > 1000 && usage <= 2000 etc.
bill = (usage_in_cubic_meters - 1000) * .018 + 15;

Maybe move the multipliers and fixed cost (.018, 15) to global
variables or so to make them easier changable. Magic numbers in the
code are evil(tm).
else if ((usage_in_cubic_meters >= 2001) && (usage_in_cubic_meters <=
3000))
bill = ((usage_in_cubic_meters - 2000) * .02 + 15 + 18);

else if (usage_in_cubic_meters >= 3001)
bill = 70;


return bill;
}

Cheers,
Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top