How can I set a decimal point in BETWEEN a number and not after it?

Sep 30, 2014 at 6:33am
I wrote this program that will calculate the retail price of an item. So far I have the program working but the output is not exactly what I expected.

For example, if I input 5.00 for the cost and 100 for the percentage(markup), the retail price should be 10.00, but instead the output gives me the result of 1000.00. I have already experimented with the iomanip library but I can't seem to figure out a way to correctly format the output to put a decimal point in between the numbers. Can anyone give me any hints on how to do this?.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>

using namespace std;


double cost, percentage, retailprice; //global variables



double calculate(double cost, double percentage) //function to calculate retail price
{
    retailprice = cost * (percentage + 100);

    return retailprice;
}

int main ()
{


    cout << "Enter the item's wholesale cost: " << "\n";
    cin >> cost;

    cout << "Enter the item's markup percentage: " << "\n";
    cin >> percentage;


    cout << setprecision(2) << fixed;
    cout << "The item's retail price is " << calculate (cost,percentage) << "\n";


    return 0;



}
Last edited on Sep 30, 2014 at 6:34am
Sep 30, 2014 at 6:42am
retailprice = cost * (percentage + 100);

retailprice = 5.00 * (100+100);

retailprice = 5.00 * 200;

retailprice = 1000.00;

Nothing wrong with the coding, it's your maths!
Sep 30, 2014 at 6:55am
Change:
retailprice = cost * (percentage + 100);
To:
retailprice = cost * (percentage/100. + 1);
Last edited on Sep 30, 2014 at 6:56am
Sep 30, 2014 at 6:57am
I don't think there's anything wrong with my "maths". That's exactly how you get the retail price: add 100 to the markup percentage and multiply the sum by the cost of the item. I still think something might be wrong with the coding. Or at least just how the output is being formatted.

Last edited on Sep 30, 2014 at 6:58am
Sep 30, 2014 at 6:59am
My maths is there plain and simple.

See Stewbond's post.
Sep 30, 2014 at 7:00am
Thank you stewbond, that was all I needed
Topic archived. No new replies allowed.