How do I perfectly allign my columns in this output?

The output looks crooked. And how do I sort the values of each distance from origin in ascending order? How do I compare the values from each generated value of distanceFromOrigin to find out if that value that is smaller than the other generated values?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
#include <cmath>
#include <random>
#include <ctime>
#include <algorithm>

using namespace std;

  class Point
{
private:
	int m_x;
	int m_y;

public:
	Point(int x = 0, int y = 0) : m_x(x), m_y(y) { }

	void setX(int x)
	{
		m_x = x;
	}

	void setY(int y)
	{
		m_y = y;
	}

	int getX()
	{
		return m_x;
	}

	int getY()
	{
		return m_y;
	}

	double distanceFromOrigin()
	{
		return std::sqrt(pow(m_x, 2) + pow(m_y, 2));
	}
};

int main()
{
	const int SIZE = 15;
	Point *myPointer;
	myPointer = new Point();

	std::default_random_engine engine{ static_cast<unsigned int>(std::time(0)) };
	std::uniform_int_distribution<int> randomIntforXorY{ -15, 15 };
	
	for (size_t i = 0; i < SIZE; i++)
	{
		std::cout << std::fixed << std::showpoint << std::setprecision(2);
		Point myPointer = randomIntforXorY(engine);
		myPointer = Point(randomIntforXorY(engine), randomIntforXorY(engine));
		cout << left << "x: " << myPointer.getX() << left;
		cout << left << "  y: " << myPointer.getY() << left;
		cout << left << "  Distance from Origin: " << myPointer.distanceFromOrigin() << left << endl;
	}
	delete myPointer;
	return 0;
}


This is the output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
x: -2  y: -8  Distance from Origin: 8.25
x: 11  y: -11  Distance from Origin: 15.56
x: 1  y: -11  Distance from Origin: 11.05
x: 2  y: 13  Distance from Origin: 13.15
x: -1  y: 6  Distance from Origin: 6.08
x: 6  y: -8  Distance from Origin: 10.00
x: -8  y: 8  Distance from Origin: 11.31
x: -11  y: -6  Distance from Origin: 12.53
x: 8  y: 15  Distance from Origin: 17.00
x: -10  y: 15  Distance from Origin: 18.03
x: -14  y: 4  Distance from Origin: 14.56
x: -13  y: 7  Distance from Origin: 14.76
x: 7  y: 8  Distance from Origin: 10.63
x: 6  y: 13  Distance from Origin: 14.32
x: 6  y: 4  Distance from Origin: 7.21
Last edited on
Hello boypetey17,

Your use of "left " is not working the way you are thinking. "std::left" or "std::right is followed by "std::setw(5)". The number in ()s sets the size of space for what follows. If you use "std::left" or "std::right" this tells the output to position what is output to the ""left " or "right" of the space set by "std::setw()".

So in your code replace the "left at the beginning of the line with "std::setw()" and the "left " at the end of the line just delete it.

The line std::cout << std::fixed << std::showpoint << std::setprecision(2); only needs to be done once, so put it above the for loop. You do not have to do it every time in the for loop. Once you set "std::fixed", "std::showpoint" and "std::setprecision(2)" these will not change until you tell the program something different.

When I changed the for loop this way:
1
2
3
4
5
6
7
8
9
10
std::cout << std::fixed << std::showpoint << std::setprecision(2);
	
for (size_t i = 0; i < SIZE; i++)
{
	Point myPointer = randomIntforXorY(engine);
	myPointer = Point(randomIntforXorY(engine), randomIntforXorY(engine));
	cout << std::setw(4) << "x: " << std::setw(4) << myPointer.getX();
	cout << std::setw(4) << "  y: " << std::setw(4) << myPointer.getY();
	cout << std::setw(25) << "  Distance from Origin: " << std::setw(6) << myPointer.distanceFromOrigin() << endl;
}

It gave me the output of:

 x:   13  y:   -1   Distance from Origin:  13.04
 x:   -9  y:   -9   Distance from Origin:  12.73
 x:    0  y:  -11   Distance from Origin:  11.00
 x:   -9  y:    3   Distance from Origin:   9.49
 x:    6  y:  -10   Distance from Origin:  11.66
 x:   -3  y:   -5   Distance from Origin:   5.83
 x:    6  y:   12   Distance from Origin:  13.42
 x:  -13  y:   -2   Distance from Origin:  13.15
 x:    7  y:    7   Distance from Origin:   9.90
 x:    3  y:  -10   Distance from Origin:  10.44
 x:    4  y:    6   Distance from Origin:   7.21
 x:  -14  y:  -11   Distance from Origin:  17.80
 x:  -10  y:  -13   Distance from Origin:  16.40
 x:    3  y:    6   Distance from Origin:   6.71
 x:  -10  y:   -7   Distance from Origin:  12.21


 Press Enter to continue


Which I think is what you are looking for. When working correctly strings are positioned left and numbers are positioned right with the idea that decimal points will line up.

One last thought: the "std::setw()" needs to be wide enough to account for maximum number of characters that will be printed. Anything shorter will be padded with spaces. You can also make the width one more than you need to adjust spacing. And do not forget to account for the minus siine on negative numbers.

Hope that helps,

Andy
Yeah, that helped. Thanks.
Topic archived. No new replies allowed.