Thread: User-defined functions

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    You could try reading cprogramming.com's tutorial on functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06: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, 12: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