fixed on one side of a line

im trying to make it so that when the program calculate, the price is in a fixed value of 2 decimal. yet when i add the line << fixed << after "@ 'price" each". the fixed is applied to the entire cout. so how do i make it so that the fixed is applied to calculating the price and not the amount of item purchase?

i.e.

2 pencils @ .90 each = $1.80
2 pens @ .65 each = $1.30

and not

2 pencils @ .90 each = $1.80
2.00 pens @ .90 each = $1.30
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double d, e; 	
	cout << "Pen @ 0.65 each; how many would you like to buy? ";
	cin >> d;
	cout << "Pencil @ 0.90 each; how many would you like to buy? ";
	cin >> e;
	double a,b,c,g,h,i,j;
	a= .65;
	b= .90;
	g= d*a;
	h= e*b;
	j= g+h;
	cout.precision(2);
	if ( d<10)
	cout << "   " << d << " Pens @ 0.65 each   = $ " << g << endl;
	if ( d>=10)
	cout << "  " << d << " Pens @ 0.65 each   = $ " << g << endl;
	if ( e<10)
	cout << "   " << e << " Pencils @ 0.90 each   = $ " << h << endl;
	if ( e>=10)
	cout << "  " << e << " Pencils @ 0.90 each   = $ " << h << endl;
	cout << "                             -----------\n";
	cout << fixed << "                     TOTAL = $ "<< j << endl;
	system("Pause");
	return 0;
}


Last edited on
That compiles and works as expected in my compiler.

However, in the last if statement, f is an undeclared variable.

And if you don't want it to show "2.00 pens", why not just declare the pen variable as an integer?
Topic archived. No new replies allowed.