Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Need help -- starting functions (http://www.velocityreviews.com/forums/t450329-need-help-starting-functions.html)

Brian 12-06-2005 09:37 PM

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?


Augusto KhaoticMind da Silva 12-06-2005 09:49 PM

Re: Need help -- starting functions
 
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;
}


Victor Bazarov 12-06-2005 09:52 PM

Re: Need help -- starting functions
 
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

Howard 12-06-2005 10:07 PM

Re: Need help -- starting functions
 

"Augusto KhaoticMind da Silva" <mkhaos7@gmail.com> wrote in message
news:1133905745.894442.161260@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



Brian 12-07-2005 04:53 AM

Re: Need help -- starting functions
 
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?


Brian 12-07-2005 05:12 AM

Re: Need help -- starting functions
 
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?


Marcus Kwok 12-07-2005 06:17 AM

Re: Need help -- starting functions
 
Brian <brian.digipimp@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

Niklas Norrthon 12-07-2005 09:17 AM

Re: Need help -- starting functions
 
"Brian" <brian.digipimp@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

Brian 12-07-2005 06:18 PM

Re: Need help -- starting functions
 
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 <brian.digipimp@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



osmium 12-07-2005 06:35 PM

Re: Need help -- starting functions
 
"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 <brian.digipimp@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

>





All times are GMT. The time now is 03:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.