User-defined functions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

User-defined functions

 
0
  #1
Apr 10th, 2007
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):
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <conio>
  5. using namespace std;
  6. int main()
  7. {
  8. double wholesaleCost, markupPercentage, markupAmount, retailPrice;
  9. cout << "What is the wholesale cost of the item? ";
  10. cin >> wholesaleCost;
  11. cout << "What is the markup percetage for this item? ";
  12. cin >> markupPercentage;
  13. markupAmount = wholesaleCost * markupPercentage;
  14. retailPrice = wholesaleCost + markupAmount;
  15. cout << fixed << showpoint <<setprecision(2);
  16. cout << "Your retail price for this item is $" <<retailPrice <<endl;
  17. getch();
  18. return 0;
  19. }

Does anyone have any idea how I can do this?? Any input is appreciated.
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: User-defined functions

 
0
  #2
Apr 10th, 2007
#include<iostream>
usingnamespace std;
#include<iomanip>
double CalculateMarkUpAmount(double, double);
double CalculateRetailPrice(double, double);
 
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 = CalculateMarkUpAmount(wholesaleCost, markupPercentage);
retailPrice = CalculateRetailPrice(wholesaleCost, markupAmount);
cout << fixed << showpoint <<setprecision(2);
cout << "Your retail price for this item is $" <<retailPrice <<endl;
cin.get();
return 0;
} double CalculateMarkUpAmount(double wholeSale, double markUpPercent) {
return wholeSale * markUpPercent;
} double CalculateRetailPrice(double wholeSale, double markUpAmount) {
return wholeSale + markUpAmount;
}

So take a look at what I did, and compare that to the long long long tutorial i wrote on how to use functions in your other post.
Last edited by mariocatch; Apr 10th, 2007 at 2:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: User-defined functions

 
0
  #3
Apr 11th, 2007
Don't forget to check for negative part..
You'll need to change the return type probably or throw an exception, if you already know how to..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC