Setw() not working

So this program is not complete and probably isn't fully set up correctly but my problem for right now is that setw() is not working correctly for me. Can someone please tell me what I did wrong?

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double hypotenuse(double,double);

int main()
{
	cout << "Triangle" << fixed << setw(6) << "Side 1" << fixed << setw(6) << "Side 2" << fixed << setw(6) << "Side 3" << endl;

	cout << "1" << fixed << setw(3) << "3.0" << fixed << setw(3) << "4.0" << hypotenuse(3.0, 4.0);


	system("pause");
	return 0;
}

double hypotenuse(double sideA, double sideB)
{
	double hyp = 0;
	hyp = sqrt(pow(sideA, 2) + pow(sideB, 2));

	return hyp;
}
Last edited on
What exactly do you mean setw isn't working? What are you expecting it to do? Are you expecting it to have 6 spaces before each display? If so then you should understand that setw doesn't work that way. In reality setw sets the minimum width of the stream. So, if what you're displaying is less than the set width you get the extra padded characters. Otherwise, the characters will appear to have no padding. If you have to use setw as a means of spacing, then you could try and account for the appropriate width. Otherwise, one fix could be that you create a string called spacing string spacing(6, ' '); and construct it with a set amount of spaces. Another thing could be to use the tab escape character '\t' as a means of spacing out your output.
Topic archived. No new replies allowed.