473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help -- starting functions

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

Dec 6 '05 #1
14 1315
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;
}

Dec 6 '05 #2
Brian wrote:
#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
Dec 6 '05 #3

"Augusto KhaoticMind da Silva" <mk*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Dec 6 '05 #4
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?

Dec 7 '05 #5
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?

Dec 7 '05 #6
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
Dec 7 '05 #7
"Brian" <br************@gmail.com> writes:
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
Dec 7 '05 #8
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


Dec 7 '05 #9
"Brian" writes:
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.
<snip>

{
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

Dec 7 '05 #10

"Brian" <br************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Dec 7 '05 #11
Brian <br************@gmail.com> wrote:
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.

--
Marcus Kwok
Dec 7 '05 #12
Marcus Kwok wrote:
Brian <br************@gmail.com> wrote:
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.

--
Marcus Kwok


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;
}

Dec 7 '05 #13

Brian wrote:
Marcus Kwok wrote:
Brian <br************@gmail.com> wrote:
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.

--
Marcus Kwok
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

Dec 7 '05 #14
> > int usage_in_cubic_meters;

ALWAYS initialize your variables...


by initialize my variables do you mean int_usage_in_cubic_meters = 0;?

Dec 8 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
by: Brett Baisley | last post by:
At school, we do all of our coding in emacs, but I am trying to get the example apps working at home using Visual C++.net. In the example, there are 4 .cpp files (canvas.cpp, main.cpp,...
16
by: Jim Langston | last post by:
I know that functions starting with an underscore, or two underscores, are reserved by the compiler/c++ and should not be used by the user and may cause undefined behavior. My question is, how...
4
by: drazbliz | last post by:
Hello, I am running the following code segment in visual studio 2005. size_t cchSize = 10*sizeof(TCHAR); LPTSTR temp = (LPTSTR)malloc(cchSize); temp = '\0'; _tcscat_s(temp, cchSize,...
1
by: da404LewZer | last post by:
hell all, i'm starting a new wiki for programming called "CoderWiki" i plan on documenting all functions of all programming languages. I have started a couple portals for C, VB, PHP, HTML,...
7
by: | last post by:
Hi to everyone! I have an Apache Webserver running on Win2000. I try to start a console application an the server though PHP, with the functions exec() or passthru() but it doesn't work. The...
2
by: masker | last post by:
I was on the web trying to find a javascript that would allow me to input a number on my website and have it increase by a given percentage every second. During my search I found the Earth...
6
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.