Out put Help

Looking for some help I have 99% of the program figured out just trying to make it look pretty. When running a math function how do you get the output to come out as currency, Ive messed with adding a decimal and 00 at the end but is there a string format or something that would make the result always hold the two decimal spaces?

Here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int calculatedRetail ()
{	
	float cost;
	float mark;
	float markup;
	float total;
	float cents=00;
	cout<<"Please enter the Cost:"<<endl;
	cin>>cost;
	cout<<"Please enter the markup value"<<endl;
	cin>>mark;
	markup=cost*mark;
	cout<<markup;
	total=cost+markup;

	cout<<"$"<<total<<endl<<endl;

	return total;
}



Hopefully I posted this right thanx again for your time
closed account (3CXz8vqX)
There is indeed. I'd recommend using stdio.h instead of iostream too... less filesize anyway...

http://www.cplusplus.com/reference/cstdio/printf/ << you'll need to include stdio.h
@ravenshade - printf has no place in C++ code.
less filesize anyway
What does that mean?

@OP - Use setprecision in your cout statement to determine the number of decimal places.

1
2
3
4
5
#include <iomanip>
using namespace std; 

//  Set fixed format with 2 decimal places. 
cout << fixed << setprecision(2);


closed account (3CXz8vqX)
Ah... lets say I test a file

1
2
3
4
5
6
#include <iostream>
int main() 
{
    cout << "text";
    return 0;
}


actually uses more bytes than...
1
2
3
4
5
6
#include <stdio.h>
int main()
{
    printf("Text");
    return 0;
}


....AbstractionAnon, I linked to a reference on cplusplus...so I beg to differ that it doesn't belong.

Hmm, looks like printf vs cout is a hugely debatable topic. There are of course downsides to printf. Depends on your needs and requirements.
Last edited on
So to understand the precision statement

 
cout<<"$"<<total<<setprecision(2)<<endl<<endl;
setprecision() has to go before you output total.
 
cout<<"$"<<setprecision(2)<<total<<endl<<endl;

Topic archived. No new replies allowed.