User-defined functions

N

Naya

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

Victor Bazarov

Naya said:
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 said:
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
 
F

Fei Liu

Naya said:
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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top