Functions Help

This is a discussion on Functions Help within the C++ Programming forums, part of the General Programming Boards category; I am trying to setup a function for the following code and am having a problem. I need to show ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    Question 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. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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()
    c)calculate cost adult()
    d)calculate cost fee()
    calculate total cost
    end calculate total()

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What exactly is your problem? Are you receiving any errors? Post the rest of your code, using code tags of course.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    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
    Attached Files Attached Files
    Last edited by NIM; 10-25-2003 at 04:46 AM.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    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. #6
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    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).

  7. #7
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    Look into downloading these powerpoint slides. (link at bottom)

    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 subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21