1. ## 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. 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.

3. ## 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. OSR:

> fxn

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

> userValues

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. 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).

6. > 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. ## well whatever

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

8. 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. 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. > 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. OneStiffRod, you can't put them inside another function because the vars will go out of scope.

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.

13. Oh, nevermind, I see what you were thinking.

14. ## what's the difference?

huh? lol

scott27349<----newbie