Using Borland 4.5, yea its old but this is what my teacher prefers.

Problems States:

Markup

Write a program that asks the user to enter and item's wholesale cost and its markup percentage. It should then display the item's retail price. For Example

If an item's wholesale cost is 5.00 and its markup percentage is 100% then the item's retail price is 10.00

If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50

The Program should have a function named calculateRetail that receives the wholesale cost and the markup percentage as arguments, and returns the retail price of the item.

Input Validation: Do not accept negative values for either the wholesale cost of the item or the markup percentage.

and this is what I have so far.

``````#include <iostream.h>
#include <iomanip.h>

void calculateRetail(double, double, double, double);
//Function where the retail price is calculated

int main()

{

double wCost;  // To hold the whole sale cost of the item
double mPercent;  // To hold the mark up price percentage of the item
double markup; // To hold the decimal form of the markup
double retailPrice; // To hold the retail price

cout << "This program finds the retail price of an item" << endl;

cout << "Enter the item's wholesale cost." << endl;
cin >> wCost;

cout << "Enter the markup percentage." << endl;
cin >> mPercent;

cout << setiosflags(ios::showpoint | ios::fixed);
cout << setprecision(2);

if (mPercent < 0 || wCost < 0)

cout << "Please enter a number above zero for both wholesale cost and" <<
cout << " markup percentage!" << endl;

else

cout << " The retail price is \$" << retailPrice << endl;

return 0;

}

///////////////////////////////////////////////////////////////////////////////
// The function takes the values of markup and whole sale cost and multiplies /
// them to get the retail cost. Before they are multiplie, the percentage is  /
// converted to decimal form so that it can be multiplied.                    /
///////////////////////////////////////////////////////////////////////////////

void calculateRetail(double wCost, double mPercent, double markup, double retailPrice)

{

markup = mPercent / 100;
retailPrice = wCost * markup;

}``````
5
Contributors
23
Replies
24
Views
7 Years
Discussion Span
Last Post by jember
Featured Replies
• 1
jonsca 1,059   7 Years Ago

main() returns an int, always. main() returns to the calling system (usually the OS), so there's no point in returning your price value that way. If you must use those ancient headers it's stdlib.h Line 26 has no meaning, especially since you have already outputted your result. Move that calculation …

• 1
jonsca 1,059   7 Years Ago

[code] #include <iomanip> // don't put .h on the header file <iomanip>. #include <conio.h> // you forgot to include [/code] You are correct about the first one, but this OP is using an older compiler which was out before the header files were established as having no .h However, conio.h …

• 1
jonsca 1,059   7 Years Ago

[QUOTE=jember] By the way what compiler are you using? I'm currently using the latest Dev C+=[/QUOTE] I use VC++ 2008 and 2010 (Express Editions, someday I'll take out a mortgage and get the full Visual Studio) or Mingw (g++ 3.4.X which I know is outdated, I'm migrating to the 4.4.X). …

• 1
jonsca 1,059   7 Years Ago

I don't know the specifics between 2005 and 2008. If I were to hazard a guess, the compiler is the same or better at complying with the standards as versions increase (some of those issues discussed [url]http://msdn.microsoft.com/en-us/library/x84h5b78(v=VS.80).aspx[/url] (drop down the menu to change versions)). I don't want to speak to …

Write a program that asks the user to enter and item's wholesale cost and its markup percentage. It should then display the item's retail price.

Based on the above requiremet, it would make sense to me to have a funtion that takes two arguments: a wholesale cost and a markup percentage...

``````//Function will return a marked-up retail price
double markup(double price, double percent)
{
//error check
if(price <= 0 || percent <= 0)
{
cout << "\n\aDomain error:  price and/or percent less than zero."
exit(1);
}

price *= percent / 100 + 10;
return price;
}

//Now you can do some of this funky stuff
cout << "A " << percent << "% markup for an item costing " << price << " is " << markup(price, percent);``````

The above code is logical and makes sense. No tricks. No special techniques. Straight-up basic c++ function. Keep it simple.

Edited by Clinton Portis: n/a

why plus 10?

Let's say you want a 50 percent markup... this is how ye' would do the math:

price = \$2
markup = 50%

So you take 50 divided by 100 which equals .5, but since you are adding a percentage to the original price, it has to be 2 * 1.5 which would be \$3.

So yes, I made an error, it should be +1 instead of +10. Good call.

Edited by Clinton Portis: n/a

Wait no im confused lol

umm do I put that and will come out? or should I still include int main ()?

also I got an error for both with and without int main ()

without int main ()

Compiling NONAME00.CPP:
Error NONAME00.CPP 19: Call to undefined function 'exit' in function markup(double,double)
Warning NONAME00.CPP 37: Function should return a value in function markup(double,double)

with int main()

Compiling NONAME00.CPP:
Error NONAME00.CPP 9: Declaration syntax error

sorry if these are dumb question I'm just a beginner.

I specifically just gave you code for 1 function.

You have to implement the code I gave you in proper c++ format, which for your project would probably be:

namespaces;

function prototypes;

main() function

function definitions

the function prototype for the one I gave you would be

``double markup(double, double);``

the function definion is

``````//Function will return a marked-up retail price
double markup(double price, double percent)
{
//error check
if(price <= 0 || percent <= 0)
{
cout << "\n\aDomain error:  price and/or percent less than zero."
exit(1);
}

price *= percent / 100 + 1;
return price;
}``````

Edited by Clinton Portis: n/a

o ok, let me try it out and see what happends. :D

ok

``````#include <iostream.h>

double markup(double, double);

main()

//Statement after calculations are done
cout << "A " << percent << "% markup for an item costing " << price << " is " << markup(price, percent);

//Function will return a marked-up retail price
double markup(double price, double percent)
{
//error check
if(price <= 0 || percent <= 0)
{
cout << "\n\aDomain error:  price and/or percent less than zero." ;
exit(1);
}

price *= percent / 100 + 1;
return price;
}``````

Got these errors

Compiling NONAME01.CPP:
Error NONAME01.CPP 8: Declaration syntax error
Error NONAME01.CPP 17: Call to undefined function 'exit' in function markup(double,double)

cout << "A" ... is line 8
exit(1); ... is line 17

o and I see you have and msn, is it ok if we chat there as it faster?

Edited by Lando_: n/a

...can anyone else help me?

you have to #inlcude<cstdlib> if you want to use exit()

your compiler error is pretty straight forward. it's telling you that you are attempting to use variable(s) that have not been declared. In essence, your compiler doesn't know how to use 'price' or 'percentage' because you have not declared them.

To remedy your error(s), simply declare 'price' and 'percentage' as double types, and initialize them to 0.

``````double price = 0.0,
percentage = 0.0;``````

Thou shalt declare all variables and initialize them before you can use them.

...can anyone else help me?

Godfather voice: I try to help you and this is what I get.. what did I do to deserve this..??

Edited by Clinton Portis: n/a

If that is the actual code that you're trying to compile then you should also be getting an error from the compiler due to the lack of braces to enclose the `main()` function.

Also, when you declare the header files you have use `#include <iostream.h>` , which is deprecated. Instead, you just need to use `#include <iostream>` , without the " `.h` " on the end.

Edited by ravenous: changed BB tags

I need .h because I am using borland 4.5
also because I am using borland 4.5 I can't use <cstdblib>

if anyone knows one of those exit things for borland 4.5 that would be great if not well can we use something else that does it similar?

this is what I have so far

``````#include <iostream.h>
#include <iomanip.h>

double markup(double, double);

double main()
{

double price, percent;

//Statement after calculations are done
cout << "This program finds the retail price of an item" << endl;

cout << "Enter the item's wholesale cost." << endl;
cin >> price;

cout << "Enter the markup percentage." << endl;
cin >> percent;

cout << setiosflags(ios::showpoint | ios::fixed);
cout << setprecision(2);

cout << "A " << percent << "% markup for an item costing " << price << " is " << markup(price, percent);
//Calculations
price *= percent /100 + 1;
return price;
}
//Function will return a marked-up retail price
double markup(double price, double percent)

{
//error check
if(price <= 0 || percent <= 0)
{
cout << "\n\aDomain error:  price and/or percent less than zero." ;
return(1);
}

}``````

I tried using the return thing but I don't think I got it right...

I can run it but it says "Floating Point: Stack Underflow"

Edited by Lando_: n/a

main() returns an int, always. main() returns to the calling system (usually the OS), so there's no point in returning your price value that way. If you must use those ancient headers it's stdlib.h

Line 26 has no meaning, especially since you have already outputted your result. Move that calculation back into your function. Think of a function just like one in math, you plug in something and you get something back out. You want to put in the price and markup and get back the price.

P.S. I know you are just starting out, but it never hurts to refresh your reading in whatever text that you have. The more (good) code you read and can emulate, the better you will become.

Edited by jonsca: n/a

I got it :D

Thank you for being patient with me!

here it is

``````#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

double markup(double, double);

int main()
{

double price, percent;

//Statement after calculations are done
cout << "This program finds the retail price of an item" << endl;

cout << "Enter the item's wholesale cost." << endl;
cin >> price;

cout << "Enter the markup percentage." << endl;
cin >> percent;

cout << setiosflags(ios::showpoint | ios::fixed);
cout << setprecision(2);

cout << "A " << percent << "% markup for an item costing " << price << " is " << markup(price, percent);

}
//Function will return a marked-up retail price
double markup(double price, double percent)

{
//error check
if(price <= 0 || percent <= 0)
{
cout << "\n\aDomain error:  price and/or percent less than zero." ;
exit(1);
}
else price *= percent /100 +1;
return price;

}``````

That's pretty good, you're almost there! I think that you just mis-understood the return statement at the end of `main()` . The idea is that you can check this return value with another program to check that your program exited correctly. This sounds a bit pointless, but if you have several programs running in series or programs that call other programs then you want to check that they did what they're supposed to. So, in general `main()` returns 0 if everything went to plan and some other number if something went wrong (this is what the "1" in your `exit(1)` statement is doing). You should be getting a warning from your compiler that there's not return value from a non-void function or something. All you need is to do is put `return 0;` on the last line of `main()` .

I think that you might want to reconsider your use of Borand 4.5, since this IDE was produced between 1994 and 1996. I think that Dev-C++ is a reasonable, and much more up-to-date replacement on Windows. (I don't use it myself, I use GCC on Linux). This will allow you to use the modern syntax for the declaration of headers and give access to more modern debugging features.

Finally, now that you've got everything working, you should consider putting in some proper exception-handling to catch the negative `price` or `percentage` conditions. This would replace the `exit(1)` statement. These days we use the `try{}... catch()` syntax, which are statements of the form:

``````try{
/* Statement(s) to try */
}
catch(my_exception &){
/* Code to run if it goes wrong */
}``````

You need to `#include <exception>` and your function would be something like:

``````double markUp(double price, double percentage) throw (my_exception)
{
if(check_conditions){
throw my_exception();
}

/* Do stuff here */
}``````

This allows you to catch the event that the user enters silly numbers without just dumping them out of the program, you could give them the opportunity to re-enter their values, for example.

Edited by ravenous: Tidied up wording

Good point about the exceptions. There are a couple of minor things I wanted to clarify, though.

You should be getting a warning from your compiler that there's not return value from a non-void function or something. All you need is to do is put return 0; on the last line of main()

This is one of those things that you would expect to be problematic, but if there is no return value specified, then according to the standard, a value of zero is assumed.

I think that Dev-C++ is a reasonable, and much more up-to-date replacement on Windows.

A push in the right direction, but Dev-C++ is no longer in development and uses an older version of g++. A more up-to-date option is Code::Blocks with the mingw version of gcc.

Ah, good point about the default return value. I had forgotten that, due to reflexively specifying it to be zero for such a long time :o)

As I said, I'm a Linux kind of person, so I don't use anything on Windows, but I'd heard that Dev-C++ was OK. If Code::Blocks is still in development, then that's obviously a better choice (or will become so in the future). Although, I didn't really take to it when I tried it in Linux. All personal choice, I guess. Either way, don't use an IDE from 1996!

Edited by ravenous: Changed wording &amp; punctuation

``````#include <iostream>
#include <iomanip> // don't put .h on the header file <iomanip>.
#include <conio.h> // you forgot to include
using namespace std;
void calculateRetail(double, double, double, double);
//Function where the retail price is calculated

int main()

{

double wCost; // To hold the whole sale cost of the item
double mPercent; // To hold the mark up price percentage of the item
double markup; // To hold the decimal form of the markup
double retailPrice; // To hold the retail price

cout << "This program finds the retail price of an item" << endl;

cout << "Enter the item's wholesale cost." << endl;
cin >> wCost;

cout << "Enter the markup percentage." << endl;
cin >> mPercent;

cout << setiosflags(ios::showpoint | ios::fixed);
cout << "\n\n";

if (mPercent <= 0 || wCost <= 0) // THIS IS THE PROBLEM

cout << "Please enter a number above zero for both wholesale cost and" << " markup percentage!" << endl;

else

cout << " The retail price is \$" << setprecision(2)<< retailPrice << endl;

getche(); // so when you run the program, it will not automatically exit
return 0;

}

///////////////////////////////////////////////////////////////////////////////
// The function takes the values of markup and whole sale cost and multiplies /
// them to get the retail cost. Before they are multiplie, the percentage is /
// converted to decimal form so that it can be multiplied. /
///////////////////////////////////////////////////////////////////////////////

void calculateRetail(double wCost, double mPercent, double markup, double retailPrice)

{

markup = mPercent / 100;
retailPrice = wCost * markup;

}``````

There's no more syntax error. I think the problem here you forgot to put if statements.
(If an item's wholesale cost is 5.00 and its markup percentage is 100% then the item's retail price is 10.00

If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50)

``````if (mPercent <= 0 || wCost <= 0)

cout << "Please enter a number above zero for both wholesale cost and" <<
cout << " markup percentage!" << endl;

else If (wCost == 5.00 && mPerceny == 100)
retialPrice = 10.00;
cout << setprecision(2)<< retailPrice << endl;
else if  (wCost == 5.00 && mPerceny == 50)
retialPrice = 7.50;
cout << setprecision(2)<< retailPrice << endl;

else
cout << " The retail price is \$" << setprecision(2)<< retailPrice << endl;``````
``````#include <iomanip> // don't put .h on the header file <iomanip>.
#include <conio.h> // you forgot to include``````

You are correct about the first one, but this OP is using an older compiler which was out before the header files were established as having no .h

However, conio.h is also a vestige. There's no need to use it, as it's non-portable. There are standard ways to emulate getche() like with a cin.ignore()/cin.get() combination (or flushing the input buffer instead of cin.ignore(), see http://www.daniweb.com/forums/post438070.html#post438070)

Edited by jonsca: n/a

thank you for the infos.. very cool :)

Cool.. thanks for the info :)
By the way what compiler are you using?
I'm currently using the latest Dev C+=

By the way what compiler are you using?
I'm currently using the latest Dev C+=

I use VC++ 2008 and 2010 (Express Editions, someday I'll take out a mortgage and get the full Visual Studio) or Mingw (g++ 3.4.X which I know is outdated, I'm migrating to the 4.4.X). They all have their strengths and weaknesses.

Dev-C++ uses an even older version of Mingw, so you're better off with Code::Blocks, or another IDE, which comes with a newer version of the compiler.

Edited by jonsca: n/a

Thank you. This is a great help for me

I see.. got any advice on what IDE should I use? and if I use VC++ 2008, the Express edition, is there any changes with codes? Currently in my school, we use either dev C++ or Visual Studio 2005. I do not want conflicts with my school works.

I don't know the specifics between 2005 and 2008. If I were to hazard a guess, the compiler is the same or better at complying with the standards as versions increase (some of those issues discussed http://msdn.microsoft.com/en-us/library/x84h5b78(v=VS.80).aspx (drop down the menu to change versions)).

I don't want to speak to the differences because many of them are nuances, but you can always test your code with multiple compilers to see the real story.

I see. Thank you very much. This is a great help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.