By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

User-defined functions

P: n/a
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):

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

Apr 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Naya wrote:
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):

#include <iostream>
#include <iomanip>
#include <conio>
There is no <coniostandard header. I doubt you need it here.
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;
Take the two statements above and move them into a separate function
(you do know how to define a function, don't you?). Make the function
accept two arguments, both of type 'double'. Make it return 'double'.

An example of a function:

int sum_of(int a, int b)
{
return a+b;
}

An example of how you'd call it:

int one_two_three = 123;
int four_five_six = 456;
int result = sum(one_two_three, four_five_six);
>
cout << fixed << showpoint <<setprecision(2);
cout << "Your retail price for this item is $" <<retailPrice
<<endl;

getch();
You can replace it with standard means of creating a delay (see
std::getline or just input a single character.
return 0;
}

Does anyone have any idea how I can do this?? Any input is
appreciated.
Does your teacher have any time you could use to ask for help? He
(or she) should be the first you go to, not the newsgroup.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '07 #2

P: n/a
Naya wrote:
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):

#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.
For example, a user defined function is declared like this:
double calculate_price(double wholesaleCost, double markupPercentage){
return wholesaleCost*markupPercentage;
}

int main(){
double a, b, c;
c = calculate_price(a, b);
}
Apr 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.