# Functions Help

• 10-24-2003
Rhino1775
Functions Help
I am trying to setup a function for the following code and am having a problem. I need to show a \$25.00 security fee for groups of 20 or more people who buy tickets. I have the "if, else" portion down I think but need a function to to the work.

if (child + youth + adult >= 20){ // in event theres20 or more poeple
cout <<" Total Bill: " <<" \$" <<CostChild + CostYouth + CostAdult + 25<<"***" <<endl <<endl;
}

else { // otherwise
cout <<" Total Bill: " <<" \$" <<CostChild + CostYouth + CostAdult <<endl <<endl;

}

cout <<" *One person is free for every 4 tickets in this age group\n";
cout <<" **The price is \$9 per adult in excess of 10 adults\n";
if (child + youth + adult >= 20){ // Gota let em know about the fee
cout<<" ***A \$25 security fee has been attached as you have 20 or more people\n";
}

cin >> any;
return 0;

Any assistance/suggestions is greatly appreciated!
Code:

`if (child + youth + adult >= 20){ // Gota let em know about the fee`
• 10-24-2003
one way is to create an overall function to calculate total cost. Within that function you call other functions to calculate each portion of the cost and then sum the returns of the different functions.

1)calculate total()
a)calculate cost child()
b)calculate cost youth()
d)calculate cost fee()
calculate total cost
end calculate total()
• 10-25-2003
bennyandthejets
What exactly is your problem? Are you receiving any errors? Post the rest of your code, using code tags of course.
• 10-25-2003
NIM

A couple pointers that I have for your program:

1) Use set width, this will eliminate readbility issues when others look at your code. [i.e. setw ( 13 )]

2) A lot of times you can initialize variables that are a group of things, then call it in the function later. Again mostly readability and will eliminate a lot of double coding.

Code:

```double numgroups20 = (a + b + c) /  20;  // initialize first cout << "The number of groups of 20 are: " << numgroups20 << endl; // imagine how much easier it is to code this way```
VS

calling out this statement four times in the function
Code:

`cout << "The number of groups of 20 are: " << ((a + b + c) / 20 )<< endl;`
3) Not too sure why you have a cin command at the end of your entire program. I could see something like this being used if you have a loop function, but you don't so I took it out.

Well, take a look at the code and test every possible solution. If this works for you, then great. I know I didn't change a lot of things. And I know I stated above the things I would change, but some of those are general pointers that will help you in future code. If you would like help in changing your current code to make the program more readable then I will be glad to help.

-NIM
• 10-25-2003
Rhino1775
NIM----THANKS!!

That was a huge help! I tested all possible solutions and seem comfortable with them.

What do you recommend I do to make it more readable?
• 10-25-2003
Dark Nemesis
Usually indentation, and commenting are used to make code more readable, comments should tell other programmers what the code is supposed to be doing, and why you did it this way opposed to another way (if possible).
• 10-25-2003
NIM