How do I properly format this table? - c++

I'm trying to properly format a table so that it prints out values below each column, beginning with the first character of that column. I've had success formatting three columns, but I'm unable to figure out how to format the K Count and LM Count columns so that they are printed out in a neat fashion.
What are some corrections I can make to the while loop portion of the code so that the K count and LM count columns are printed out neatly?
void printTable(const vector<int>& z, const vector<long>& x, const
vector<long>& y,
const vector<int>& a, const vector<int>& b)
{
ostringstream ss;
ss << "\n\n\n" << setw(10) << left << "Digits" << "Input Numbers "
<< setw(11) << right << "K Output " << setw(6)
<< right << "K Count " << setw(10) << right << "LM Output " << setw(6)
<< right << "LM Count" << endl;
int i = 0, n = 0;
while (i < 5)
{
string q = to_string(z[2*n]) + " x " + to_string(z[abs(2*n + 1)]);
string r = to_string(x[i]);
string s = to_string(a[i]);
string t = to_string(y[i]);
string u = to_string(b[i]);
ss << setw(10) << left << (i+1) << q
<< setw(16) << right << r
<< setw(11) << right << s
<< setw(12) << right << t
<< setw(10) << right << u << endl;
i++;
n++;
}
string r = ss.str();
cout << r;
}

You're printing the following:
- Digits left aligned over 10 chars
- Directly followed by the input number without specifying the width
- K output right aligned over 16 chars
You should instead specify the width of the input numbers as well.
Try this:
ss << setw(10) << left << (i+1) <<
setw(16) << left << q <<
setw(10) << right << s <<
setw(12) << right << t <<
...
The exact width of each column may not be correct. Try it out yourself.

To fix K Count output, you should reset the cursor to align left after setting the width for the previous result, then shift back right. Something like this should do what you want.
ss << setw(10) << left << (i+1) << right
setw(16) << left << q << right <<
setw(11) << left << r << right <<
setw(12) << left << s << right <<
setw(12) << left << t << right <<
setw(10) << left << u << right << endl;

Related

Aligning output column using setw in C++

I have been reading about setw for column widths, however setting a width for each colum does not seem to be aligning the last two columns. I have tried using the right align, however it also changes alignment for the left two columns as well, which I don't want. In the end I'd like everything to be left aligned. Is there an easier way of doing this other than setw?
Current code:
void print_head()
{
cout.setf(ios::left);
cout << setw(16) << "Operation"
<< setw(32) << "z"
<< setw(8) << "Cost"
<< setw(8) << "Total" << endl;
for (int i=0; i<64; ++i) cout << "-";
cout << endl;
}
print_head();
cout.setf(ios::left);
cout << setw(16) << "Initial String"
<< setw(32) << test1
<< setw(8) << "0"
<< setw(8) << "0" << endl;
for (int g = minops[0] + minops[1] - 1;g>-1;g--){
string a;
for(stringstream ss(operations[g]); getline(ss, a, ','); ) // that's all !
v.push_back(a);
//cout << v[current] << "\n";
if (v[current] == "c"){
z = sright(z, cursorg);
current++;
cout << setw(16) << "right"
<< setw(32) << z
<< setw(8) << "0"
<< setw(8) << tcost << endl;
}else if (v[current] == "d"){
z = sdelete(z, cursorg);
size = size - 1;
tcost = tcost + 2;
cout << setw(16) << "delete"
<< setw(32) << z
<< setw(8) << "2"
<< setw(8) << tcost << endl;
//cout << "cursor out of delete: " << cursorg << "\n";
current = current + 2;
}else if (v[current] == "r"){
z = sreplace(z, cursorg, test2, stoi(v[current+1]), stoi(v[current+2]));
int printtemp = stoi(v[current+2]);
string s(1,test2[printtemp]);
string tempstr = "replace by " + s;
tcost = tcost + 4;
cout << setw(16) << tempstr
<< setw(32) << z
<< setw(8) << "4"
<< setw(8) << tcost << endl;
current = current + 3;
}else if (v[current] == "i"){
z = sinsert(z, test2, cursorg, stoi(v[current+1]));
size = size - 1;
tcost = tcost + 3;
int printtemp = stoi(v[current+1]);
string s(1,test2[printtemp]);
string tempstr = "insert " + s;
cout << setw(16) << tempstr
<< setw(32) << z
<< setw(8) << "3"
<< setw(8) << tcost << endl;
current = current + 2;
}else{
}
//cout << operations[g] << "\n";
//cout << "z is: " << z << "\n";
}
cout << "\n";
cout << "minimum operations is: " << minops[0] << "\n";
This is my current output, however I want the cost and total cost to be a neat aligned column under the header. How do I use setw to fix my alignment?
Delete implementation:
string sdelete(string input, int &cursor){ //deletes the letter under the cursor, cost 2
if (cursor == (input.length() + 1)){
//do nothing
}else{
input[cursor] = NULL;
cursor = cursor+1;
}
//DEBUG cout << "delete input: " << input << "\n";
//DEBUG cout << "cursor in delete: " << cursor << "\n";
return input;
}

How can I display a $ sign with a value while using setw and setprecision in c++

I want to display the dollar sign next to its value in the second column, but if I convert the value into a string, the setprecision doesn't work and it displayed more decimals than I would like. Currently the formatting doesn't look good.
My current code:
string unit = "m";
double width = 30.123;
double length = 40.123;
double perimeter = 2 * width + 2 * length;
double area = width * length;
double rate = (area <= 3000) ? 0.03 : 0.02;
double cost = area * rate;
const int COLFMT = 20;
cout << fixed << setprecision(2);
cout << setw(COLFMT) << left << "Length:"
<< setw(COLFMT) << right << length << " " << unit << endl;
cout << setw(COLFMT) << left << "Width:"
<< setw(COLFMT) << right << width << " " << unit << endl;
cout << setw(COLFMT) << left << "Area:"
<< setw(COLFMT) << right << area << " square" << unit << endl;
cout << setw(COLFMT) << left << "Perimeter:"
<< setw(COLFMT) << right << perimeter << " " << unit << endl;
cout << setw(COLFMT) << left << "Rate:"
<< setw(COLFMT) << right << rate << "/sqaure" << unit << endl;
cout << setw(COLFMT) << left << "Cost:"
<< setw(COLFMT) << right << "$" << cost << endl;
Produces this poorly formatted output:
Length: 40.12 m
Width: 30.12 m
Area: 1208.63 square m
Perimeter: 140.49 m
Rate: 0.03/square m
Cost: $36.26
"Currently the formatting doesn't look good."
That's because std::right pertains to what follows it, in your case "$". So the dollar sign is right-aligned and not the value that follows on afterwards.
What you want is the fully formatted monetary value "$36.26" to be right aligned. So build that up as a string first with stringstream.
stringstream ss;
ss << fixed << setprecision(2) << "$" << cost;
cout << left << "Cost:" << setw(COLFMT) << right << ss.str() << endl;

C++ setup columns using cout

So i'm just starting to learn c++ and i'm curious if its a way to formate your output with cout so it will look nicely and structured in columns
for example.
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << "Name" << setw(30) << "Height[m]" << endl;
cout << fname + " " + lname << right << setw(20) << setprecision(2) << fixed << height << endl;
cout << fname2 + " " + lname2 << right << setw(20) << setprecision(2) << fixed << height2 << endl
The output looks like this:
Name Height[m]
testname 123 1.60
short 123 1.80
I want it to look like this:
Name Height[m]
testname 123 1.60
short 123 1.80
The problem i'm trying to solve is that i want to place height at a specific position from name but depending what length of name i take the value of height either gets far away to the right or will be very close to the left. Is there a way to fix this?
First of all, with an output stream like std::cout, you cannot travel back in time and modify output which was already performed. That makes sense -- just imagine std::cout wrote into a file because you launched your program with program.exe > test.txt, and test.txt was on a USB drive which has been disconnected in the meanwhile...
So you have to get it right immediately.
Basically, there are two ways to do so.
You can assume that no entry in the first column will ever be wider than a certain number of characters, which is what you have attempted. The problem is that your setw is at the wrong position and that right should be left. A stream manipulator must be placed before the elements which should be affected. And since you want left-aligned columns, you need left:
cout << left << setw(20) << "Name" << "Height[m]" << endl;
cout << left << setw(20) << fname + " " + lname << setprecision(2) << fixed << height << endl;
cout << left << setw(20) << fname2 + " " + lname2 << setprecision(2) << fixed << height2 << endl;
But this solution is not very general. What if you'll have a name with 21 characters? Or with 30 characters? Or 100 characters? What you really want is a solution in which the column is automatically set only as wide as necessary.
The only way to do this is to collect all entries before printing them, finding the longest one, setting the column width accordingly and only then print everything.
Here is one possible implementation of this idea:
std::vector<std::string> const first_column_entries
{
"Name",
fname + " " + lname,
fname2 + " " + lname2
};
auto const width_of_longest_entry = std::max_element(std::begin(first_column_entries), std::end(first_column_entries),
[](std::string const& lhs, std::string const& rhs)
{
return lhs.size() < rhs.size();
}
)->size();
// some margin:
auto const column_width = width_of_longest_entry + 3;
std::cout << std::left << std::setw(column_width) << "Name" << "Height[m]" << "\n";
std::cout << std::left << std::setw(column_width) << fname + " " + lname << std::setprecision(2) << std::fixed << height << "\n";
std::cout << std::left << std::setw(column_width) << fname2 + " " + lname2 << std::setprecision(2) << std::fixed << height2 << "\n";
The next step of evolution would be generalising the std::vector into a self-written class called Table and iterating that Table's rows in a loop in order to print the entries.
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << left << setw(30) << "Name" << left << "Height[m]" << endl;
cout << left << setw(30) << fname + " " + lname << right << setw(6) << setprecision(2) << fixed << height << endl;
cout << left << setw(30) << fname2 + " " + lname2 << right << setw(6) << setprecision(2) << fixed << height2 << endl;

Dynamic alignment issue

When I try to list details of all items, each on a different line
with line numbering, there is alignment issue on it. I want instantly put the close bracket after the line numbering. Thanks.
cout << left
<< setw(20) << " Item Code"
<< setw(50) << "Description"
<< setw(20) << "Quantity on hand"
<< setw(20) << "Cost price"
<< setw(20) << "Selling price(RM)"
<< setw(20) << "Status"
<< setw(20) << "Discount(%)" << endl;
for (int i = 0; i <= 100; i++)//counter - 1; i++)
{
cout << left
<< setw(2) << i + 1 << ")"
<< setw(20) << item[i].getItemCode()
<< setw(50) << item[i].getDescription()
<< setw(20) << item[i].getQuantity()
<< setw(20) << item[i].getCostPrice()
<< setw(20) << item[i].getSellPrice()
<< setw(20) << item[i].getStatus()
<< setw(20) << item[i].getDiscount() << endl;
}
The only way of doing this, as far as I can see, is to walk through the list and find out "how long does this get" for each of the columns, and track what the largest is for each of the columns. Then use those values in the column width.
Strings are easy to find the length of, since they have a length as such. Numbers are harder - basically, you have to either take the approach of dividing it by ten down until it's zero (this means the integer part of floating point numbers - presumably for something like this, you have a fixed number of decimals or use "integeer to represent prices in cents" or some such). You may be able to use the std::tostring to produce as string that has a length too. Or you can use stringstream to output to a string - either individual items, or the whole lot and then count the number of characters between some separator character [that doesn't occur in the normal output, or things go wrong pretty easily!]
Example, using a simple struct:
struct Data
{
int x;
string y;
float z;
}
...
Data d[10];
int maxLen[3] = { 0 };
... // code fills in data with stuff.
for(int i = 0; i < 10; i++)
{
stringstream ss;
ss << left << d[i].x << " " << d[i].y << " " << fixed << setprecision(2) << d[i].z;
// Number of elements = 3.
for(int j = 0; j < 3; j++)
{
string s;
ss >> s;
if (s.length() > maxLen[j])
maxLen[j] = s.legnth;
}
}
...
for(int i = 0; i < 10; i++)
{
cout << left << setw(3) i << ": "
<< setw(maxLen[0]+1) << d[i].x
<< setw(maxLen[1]+1) << d[i].y
<< setw(maxLen[2]+1) << fixed << setprecision(2) << d[i].z << endl;
}

can't get setw to line up

I'm not sure how to go about lining this up better. I thought by using L = name.length(); and subtracting that from the setw it would line up better but nope. I have name and ID set correctly but I can't seem to get the rest to line up. These are floats btw.
for (int z=0; z<i ;z++){
if( EInfo[z].valid >0){
string name = employee[z].getName();
int L = name.length();
payReportObj << fixed << setprecision (2) << endl;
payReportObj << setw(3) << employee[z].getID()
<< setw(36)
<< employee[z].getName()
<< setw(30-L) << "$" << setw(4) << EInfo[z].grossPaySE
<< setw(25) << "$" << setw(4) << EInfo[z].taxSE
<< setw(15) << "$" << setw(4) << EInfo[z].insuranceSE
<< setw(25) << "$" << setw(4) << EInfo[z].netPaySE;
I'm attempting to get it more like this...
This is what I have...
Replace setw(30-L) with setw(30). And this:
int L = name.length();
is not needed. If you want to right-align numbers, use std::right.
It seams that you opened your output file in nodepad with proportional font. You won't see columns aligned with proportional font even if they are. Change font to fixed or type in your terminal type output.txt.