-
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:
- Create a function that accepts the wholesale cost and markup percentage as arguments
- Returns the retail price of the item
- 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.
-
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.
-
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.
-
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.
-
I am confused now. I'm sorry. I'm new at this.
So, what do I do??
-
You could try reading cprogramming.com's tutorial on functions.
-