User-defined functions

This is a discussion on User-defined functions within the C++ Programming forums, part of the General Programming Boards category; Hi. I am trying to write a program that asks for the wholesale cost of an item and its markup ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Unhappy User-defined functions

    Hi.

    I am trying to write a program that asks for the wholesale cost of an item and its markup percentage, and displays the retail price.

    Well, I did that the regular way, but...
    My instructions require for me to:
    1. Create a function that accepts the wholesale cost and markup percentage as arguments
    2. Returns the retail price of the item
    3. Do not accept negative values for either the wholesale cost of the item or the percent markup.


    I have read my book, but I have no idea how to create a function for wholesaleCost and markupPrice. I tried, but I got an error.

    Here is my regular program (without the user-defined function):
    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio>
    using namespace std;
    
    int main()
    {
            double wholesaleCost, markupPercentage, markupAmount, retailPrice;
    
            cout << "What is the wholesale cost of the item? ";
            cin  >> wholesaleCost;
            cout << "What is the markup percetage for this item? ";
            cin  >> markupPercentage;
    
            markupAmount = wholesaleCost * markupPercentage;
            retailPrice = wholesaleCost + markupAmount;
    
            cout << fixed << showpoint <<setprecision(2);
            cout << "Your retail price for this item is $" <<retailPrice <<endl;
    
            getch();
            return 0;
    }
    Does anyone have any idea how I can do this?? Any input is appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Basically, you want to write a function that has two parameters of type double, returns a double, and performs validation of its arguments by checking that they are non-negative.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Yep, that's basically it.

    I was thinking of something like:

    Code:
    void wholesaleCost(double); 
    void markupPercentage(double);
    I don't know how to perform the validation part.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You are creating two functions that each take one parameter of type double, and which return void. That is very different from one function that takes two parameters of type double, and which returns a double.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    I am confused now. I'm sorry. I'm new at this.

    So, what do I do??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You could try reading cprogramming.com's tutorial on functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Okay, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined header files
    By sidu in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 07:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 01:41 AM
  4. Piecewise defined discontinuous functions in Mathematica
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-16-2005, 01:24 PM
  5. functions defined as a string
    By river-wind in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 10:36 AM

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