Need help -- starting functions

Discussion in 'C++' started by Brian, Dec 6, 2005.

  1. Brian

    Brian Guest

    #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?
    Brian, Dec 6, 2005
    #1
    1. Advertising

  2. 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;
    }
    Augusto KhaoticMind da Silva, Dec 6, 2005
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Dec 6, 2005
    #3
  4. Brian

    Howard Guest

    "Augusto KhaoticMind da Silva" <> wrote in message
    news:...

    > 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
    Howard, Dec 6, 2005
    #4
  5. Brian

    Brian Guest

    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, Dec 7, 2005
    #5
  6. Brian

    Brian Guest

    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, Dec 7, 2005
    #6
  7. Brian

    Marcus Kwok Guest

    Brian <> 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
    Marcus Kwok, Dec 7, 2005
    #7
  8. "Brian" <> 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
    Niklas Norrthon, Dec 7, 2005
    #8
  9. Brian

    Brian Guest

    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 <> 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
    Brian, Dec 7, 2005
    #9
  10. Brian

    osmium Guest

    "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 <> 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, Dec 7, 2005
    #10
  11. Brian

    Howard Guest

    "Brian" <> wrote in message
    news:...
    > 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
    Howard, Dec 7, 2005
    #11
  12. Brian

    Marcus Kwok Guest

    Brian <> 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
    Marcus Kwok, Dec 7, 2005
    #12
  13. Brian

    Brian Guest

    Marcus Kwok wrote:
    > Brian <> 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;
    }
    Brian, Dec 7, 2005
    #13
  14. Brian

    Guest

    Brian wrote:
    > Marcus Kwok wrote:
    > > Brian <> 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, 2005
    #14
  15. Brian

    Brian Guest

    > > int usage_in_cubic_meters;
    >
    > ALWAYS initialize your variables...


    by initialize my variables do you mean int_usage_in_cubic_meters = 0;?
    Brian, Dec 8, 2005
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  2. MehariK
    Replies:
    4
    Views:
    308
    MehariK
    Mar 10, 2008
  3. jmDesktop

    Need Help Starting Out

    jmDesktop, Mar 18, 2008, in forum: Python
    Replies:
    6
    Views:
    245
    Peter Decker
    Mar 19, 2008
  4. gb345
    Replies:
    8
    Views:
    309
    Mike Orr
    Mar 18, 2010
  5. Corobori

    Need some hints before starting to change a page

    Corobori, Mar 2, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    110
    Ken Cox [Microsoft MVP]
    Mar 4, 2005
Loading...

Share This Page