# Thread: Functions Help

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

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

3. What exactly is your problem? Are you receiving any errors? Post the rest of your code, using code tags of course.

4. Ok, I downloaded your .cpp file from your other post on the same topic.

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

5. 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?

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

Once you have done that open chapter 2 and scroll and find slide number 56. That shows a great example of using setw and setprecision.

Like Dark Nemesis said, commenting and indentation make the programs more readable and will help even you understand what is happening.

It will save you some headaches if you have to go back over code for a logical problem later. And will make your program very portable in case others will use the code.

http://www.deitel.com/books/cppHTP4/cppHTP4_ppt.html

-NIM

If you have any further questions feel free to post them.

Popular pages Recent additions