Thread: what's the difference?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    19

    what's the difference?

    well...this is the program that i had.....

    /* Calculate the total charges and the total hours for the
    parking garage. */

    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    void calculatecharges (double hoursa, double hoursb, double hoursc);

    int main()
    {
    double a, b, c;

    cout<<"Enter the number of hours for CAR 1:";
    cin>>a;

    cout<<"Enter the number of hours for CAR 2:";
    cin>>b;

    cout<<"Enter the number of hours for CAR 3:";
    cin>>c;

    calculatecharges(a, b, c);

    return 0;
    }

    void calculatecharges(double hoursa, double hoursb, double hoursc)
    {
    double costa = 0;
    double costb = 0;
    double costc = 0;
    double totalhours = 0;
    double totalcost = 0;

    if(hoursa <= 3)
    costa = 2;
    else if (hoursa >= 17)
    costa = 10;
    else
    costa = 2 + ((hoursa-3) * .5);

    if(hoursb <= 3)
    costb = 2;
    else if (hoursb >= 17)
    costb = 10;
    else
    costb = 2 + ((hoursa-3) * .5);

    if(hoursc <= 3)
    costc = 2;
    else if (hoursc >= 17)
    costc = 10;
    else
    costc = 2 + ((hoursc-3) * .5);

    totalhours = hoursa + hoursb + hoursc;
    totalcost = costa + costb + costc;
    cout<<"CAR 1 CHARGES: "<<costa<<endl;
    cout<<"CAR 2 CHARGES: "<<costb<<endl;
    cout<<"CAR 3 CHARGES: "<<costc<<endl;
    cout<<"TOTAL: "<<totalhours<< " hours, $"<<totalcost<<endl;
    }

    and now i'm told that i have to do the same program, but use 3 functions; one to calculate the charges, one to calculate the total hours, and one to calculate the total charges. this time though, i can't use global variables. my question is...what's the difference? doesn't my program fit to the new criteria? also...i still have an error with that function...why? i don't know. it just won't compile without returning an error.

  2. #2
    The teach wants you to break up the program into smaller, more usable pieces, which is good programming practice. You need to turn that huge function you have into 3 separate functions and then your gonna need a 4th function to communicate to the user so you aren't using GLOBAL variables.

    Your Main function will look like 1 or 2 function calls with maybe a little bit of if statements on the side with salad.

    fxn userValues(); //get user values, COUT and CIN
    fxn calcCharges(); //calculate the charges
    fxn calcTotHrs(); //calculate the total hours
    fxn calcTotCharges(); //calculate the total charges

    int main(){
    //All calculations fxns are called within userValues() fxn
    userValues();
    }

    See, now that's much cleaner code.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    19

    so is this anywhere close to what i'm supposed to do?

    /* Calculate the total charges and the total hours for the
    parking garage. */

    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    fxn userValues(a, b, c);
    fxn calcCharges(double hoursa, double hoursb, double hoursc);
    fxn calcTotalHrs(double hoursa, double hoursb, double hoursc);
    fxn calcTotalCharges(double hoursa, double hoursb, double hoursc);

    int main()
    {
    userValues a, b, c;

    cout<<"Enter the number of hours for CAR 1:";
    cin>>a;

    cout<<"Enter the number of hours for CAR 2:";
    cin>>b;

    cout<<"Enter the number of hours for CAR 3:";
    cin>>c;

    calcCharges(double hoursa, double hoursb, double hoursc);
    calcTotalHrs(double hoursa, double hoursb, double hoursc);
    calcTotalCharges(double hoursa, double hoursb, double hoursc);

    return 0;
    }

    void calcCharges(double hoursa, double hoursb, double hoursc);
    {
    double costa = 0;
    double costb = 0;
    double costc = 0;
    double totalhours = 0;

    if(hoursa <= 3)
    costa = 2;
    else if (hoursa >= 17)
    costa = 10;
    else
    costa = 2 + ((hoursa-3) * .5);

    if(hoursb <= 3)
    costb = 2;
    else if (hoursb >= 17)
    costb = 10;
    else
    costb = 2 + ((hoursa-3) * .5);

    if(hoursc <= 3)
    costc = 2;
    else if (hoursc >= 17)
    costc = 10;
    else
    costc = 2 + ((hoursc-3) * .5);
    }

    void calcTotalHrs(double hoursa, double hoursb, double hoursc);

    totalhours = hoursa + hoursb + hoursc;
    }

    void calcTotalCharges(double hours, double hoursb, double hoursc);
    {
    double costa = 0;
    double costb = 0;
    double costc = 0;
    double totalcost =0;

    if(hoursa <= 3)
    costa = 2;
    else if (hoursa >= 17)
    costa = 10;
    else
    costa = 2 + ((hoursa-3) * .5);

    if(hoursb <= 3)
    costb = 2;
    else if (hoursb >= 17)
    costb = 10;
    else
    costb = 2 + ((hoursa-3) * .5);

    if(hoursc <= 3)
    costc = 2;
    else if (hoursc >= 17)
    costc = 10;
    else
    costc = 2 + ((hoursc-3) * .5);

    totalcost = costa + costb + costc;

    cout<<"CAR 1 CHARGES: "<<costa<<endl;
    cout<<"CAR 2 CHARGES: "<<costb<<endl;
    cout<<"CAR 3 CHARGES: "<<costc<<endl;
    cout<<"TOTAL: "<<totalhours<< " hours, $"<<totalcost<<endl;
    }

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    OSR:

    > fxn

    What the hell is this? That's not how you declare a return value... Most of these should be declared void

    > userValues

    When was this made a datatype?

    iluvmyafboys:
    The reason he didn't want you to use global variables is so you would use pointers.

    Also, when you're writing your functions, like:

    void calcCharges(double hoursa, double hoursb, double hoursc);
    {
    //stuff
    }

    Don't put a semicolon on the end of the first line. You want it like this:

    void calcCharges(double hoursa, double hoursb, double hoursc)
    {
    //stuff
    }

    This is also messed up, but I'll leave it to you to figure out why...

    calcCharges(double hoursa, double hoursb, double hoursc);
    calcTotalHrs(double hoursa, double hoursb, double hoursc);
    calcTotalCharges(double hoursa, double hoursb, double hoursc);

  5. #5
    fxn stands for function, the return types are his business along with the code, my advice was in psuedo code. I'm not going to do his homework for him.

    userValues() along with the others are the function names, they are not datatypes.

    Gotchavez is right about not using the ';' semi-colon after your function definitions.

    But no GLOBAL variables doesn't mean use pointers, it means that the prof. wants you to get rid of the variables you have in the main() function. You do this by buring those variables inside another function(userValues).
    Last edited by OneStiffRod; 02-27-2002 at 09:59 AM.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > fxn stands for function, the return types are his business along with the code, my advice was in psuedo code. I'm not going to do his homework for him.


    Alright - point taken - since you didn't say it was in pseudocode, I (and this person, apparently) thought it was some sort of a return value.... My bad...

    > userValues() along with the others are the function names, they are not datatypes.

    I see that... Not the way they were using it, though... My bad, again (man, this is really, really, not my day)

    Sorry

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    19

    well whatever

    i'm still more confused now than i was before.

  8. #8
    Unregistered
    Guest
    In order to use a function the compiler needs to see the function declaration before a function call, just like it needs to see a variable declaration before it can use a variable. Function declarations are usually placed after the list of #includes and before main(). The prototype for a function declaration is:

    returnTypeHere functionNameHere(parameterTypesHere);

    Note the ; at the end of this line.

    Function definitions can be placed either before main() or after mani(), depending on your philosophy. Here is the generic prototype for a function definition:

    returnTypeHere functionNameHere(parameterTypeHere parameterNameHere)
    {
    //function body here
    }

    Note that the first line of the function definition looks exactly like the function declaration EXCEPT that you HAVE to include the parameter name in the definition (the parameter name is optional in the declaration); AND you DON'T have a semicolon at the end of the line like you do for the declaration.

    //Sample program using (silly) function

    #include <iostream.h>

    //function declaration
    void display();

    int main()
    {
    //function call
    display();

    return 0;
    }

    //function definition
    void display()
    {
    cout << "Good luck" << endl;
    }

    return type and parameter type can be any type valid for the program.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    You don't need to do prototypes if the function is declared prior to the function that calls it.

    ie:
    Code:
    #include <stdio.h>
    
    int doHello(void)
    {
     printf("Hello, world");
     return 0;
    }
    
    int main(void)
    {
     doHello();
    }

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > You don't need to do prototypes if the function is declared prior to the function that calls it.

    Yeah, but I think it's good practice to put prototypes befor main, and the actual function after it.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    OneStiffRod, you can't put them inside another function because the vars will go out of scope.

  12. #12
    It doesn't matter that the vars go out of scope becuase by that time they will have been printed to the screen(cout). Getting rid of the GLOBAL vars means removing them from the scope of the main() fxn. That's why we moved the vars to the new function, he can treat the new fxn as if it were main() and do everything he needs to do in that fxn and than just call that fxn in main().

    If there was any vars we really wanted to keep their value we would be using classes.

    GLOBAL vars are rarely used in programs, a good programmer always tries to scope them by putting them inside of functions or putting them in structs or classes.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Oh, nevermind, I see what you were thinking.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    98

    what's the difference?

    huh? lol


    scott27349<----newbie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM