Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use iomanip's setw, setfill, and left/right? Setfill isn't stopping its output

Tags:

c++

iomanip

setw

I'm trying to get my output to look like this:

size       time1       time2
-------------------------------
10         4           8
100        48          16
1000       2937        922
10000      123011      3902
100000     22407380    830722

And I know I need to use setw(), setfill(), and left. However, my attempts keep giving me incorrect output. Here is one example of my code:

std::cout << "size" << std::setw(20) << "time" << std::setw(20) << "time2\n";
std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl;
run = 10;
for(int i = 0; i < 5; i++) {
    std::cout << run;
    run *= 10;
    std::cout << std::setw(20) << std::left << time1[i];
    std::cout << std::setw(20) << std::left << time2[i] << "\n";
}

And here's the output:

size    time1    time2
------------------------------------------------------------
103-------------------13------------------
100171-----------------199-----------------
100013183---------------667-----------------
10000670130--------------8205----------------
10000014030798-------------1403079888---------

I've tried changing the order that I'm using setw(), setfill(), and left, but I'm just flying blind right now. I've also searched iomanip tutorials. I'm following the directions--as far as I can tell--but I'm still not getting it.

How do I stop the setfill() from running over? How do I justify left but use setw() to stop the numbers from running into each other?

like image 490
user3448821 Avatar asked Mar 26 '14 03:03

user3448821


People also ask

How does SETW and Setfill work?

setw: Setw function sets the field width or number of characters that are to be displayed before a particular field. Setfill: Setfill function is used to fill the stream with char type c specified as a parameter.

What does Setfill () do?

The setfill() method of iomanip library in C++ is used to set the ios library fill character based on the character specified as the parameter to this method. Parameters: This method accepts c as a parameter which is the character argument corresponding to which the fill is to be set.

What does Setfill (' 0 ') do?

It is used to sets c as the stream's fill character.


2 Answers

How about:

std::cout << "size" << std::setw(20) << "time" << std::setw(20) << "time2\n";
std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl;
run = 10;
std::cout << std::setfill(' ');  //fill with spaces
for(int i = 0; i < 5; i++) {
    std::cout << std::setw(20) << std::left << run;  // fill the run column
    run *= 10;
    std::cout << std::setw(20) << std::left << time1[i];
    std::cout << std::setw(20) << std::left << time2[i] << "\n";
}
like image 189
sj0h Avatar answered Sep 24 '22 09:09

sj0h


sj0h's answer is great except the titles don't quite line up. To fix it I started the title line with "left" and "setw", I also had to end with "endl" instead of "\n".

  std::cout << std::left << std::setw(20) << "size" << std::setw(20) << "time" << std::setw(20) << "time2" << std::endl;

  std::cout << std::setfill('-') << std::setw(60) << "-" << std::endl;
  run = 10;
  std::cout << std::setfill(' ');  //fill with spaces
  for(int i = 0; i < 10; i++) {
      std::cout << std::setw(20) << std::left << run;  // fill the run column
      run *= 10;
      std::cout << std::setw(20) << std::left << time1[i];
      std::cout << std::setw(20) << std::left << time2[i] << std::endl;
  }
like image 43
vayu Avatar answered Sep 22 '22 09:09

vayu