how do i get numerical answers in 000.00 format?!

Write your question here.

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
38
39
40
41
42
#include "stdafx.h"

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

	int a,b,c,d,e; 
	float C,F,A,r,pi,h; 

	a = 1 ;  
	b = 5 ;
	c = 4 ;
	d = 2;
	e = 3 ;

	C = 9.12;
	F = 7.15;
	A = 5.72;
	r = 3.56;
	pi = 3.14;
	h = 0.5555555556;

	cout << "\t\t    COXCH";
	cout << "\n\t\t\t\t VC";
	cout << "\n\n\t\t\t\t     CS V30";
	cout << "\n\t\t\t\t  Beginning C++";
	cout << "\n\n\t\t\t\t  John Doe\n\n";
	std::cout << std::setfill('0') << std::setw(6) << std::setprecision(4);
	cout << "\n    The value of a from the equation a-squared = b-squared + c-squared = " << b*b + c*c;
	cout << "\n\n    The value of c from a+b divided by d+e    \t\t\t       = " << (a+b)/(d+e);
	cout << "\n\n    The value of C from C = 5/9*(F-32)  \t\t\t       = " << h *(F-32);
	cout << "\n    The value of r from A = pi times r-squared\t\t\t       = " << pi*(r*r); 
	cout << "\n\n\n";
	
	

	return 0;
  


first time posting. i need help. when i run i want the answers to be in XXX.XX format. for example if my answer is 1 i would like it to be displayed as 001.00 if you guys could help me out that would be great. FYI i included this part //std::cout << std::setfill('0') << std::setw(6) << std::setprecision(4);// cause someone on another forum told me it would work but doesnt.
The problem is that after you do std::cout << something;, std::cout "resets" the width (the minimum number of characters to output).

So you'll have to reset the width every time before you output a floating-point number:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::fixed // Fixed-point notation
              << std::setprecision(2) // 2 digits after the decimal
              << std::setw(6) // Output at least 6 characters
              << std::setfill('0'); // Add zeros at the beginning if there are < 6 characters
    
    std::cout << 1.0 << '\n';
    std::cout << std::setw(6) << 21.3 << '\n';
    std::cout << std::setw(6) << 3.14159265 << '\n';
    std::cout << 1.6;
}

Output:
001.00
021.30
003.14
1.60
thanks a lot!
Topic archived. No new replies allowed.