c++setw

C++ setw() function does not cater for character output width on screen


IDE used Eclipse

It is said to use setw() and setfill() function to format your text using cout . I tried that but if you see the text does not align on the screen because lets say in first line what compiler does is

type Reg No. and fill rest with - characters

but if you see image attached link, width of R character etc is not equal to - character's width that is why the output is misaligned, is it eclipse output setting ? (The output is aligned if it is copy pasted in NOTEPAD++ text file why ?) . Can someone please tell me how to fix it ?

I want it aligned no matter how many column or if - character is replaced with space character

     #include<iostream>
     #include<string>
     #include<iomanip>
      using namespace std;
      int main(){

       // Weird. Size of R character on screen is not equal to - that is why misalignment
       const char separator    = '-';
       const int nameWidth     = 20;
       const int numWidth      = 20;

        // Headings
        cout << left << setw(nameWidth) << setfill(separator) <<"Reg No." << left << setw(nameWidth) 
       << setfill(separator) << "First Name" << left << setw(nameWidth) << setfill(separator) <<
                "Last Name"<< endl;

        // Data column

        cout << left << setw(numWidth) << setfill(separator) << 123 << left << setw(numWidth) << 
       setfill(separator) << "JohnPeter" << left << setw(numWidth) << setfill(separator) <<
                        "" << left << setw(numWidth) << endl;

        cout << left << setw(numWidth) << setfill(separator) << 123123 << left << setw(numWidth) << 
       setfill(separator) << "Peter" << left << setw(numWidth) << setfill(separator) <<
                                "testingLastName" << left << setw(numWidth) << endl;


        cout << left << setw(numWidth) << setfill(separator) << 11233333 << left << setw(numWidth) << 
       setfill(separator) << "a" << left << setw(numWidth) << setfill(separator) <<
                                "a" << left << setw(numWidth) << endl;


      return 0;
      }

code output is not aligned see eclipse c++ output image Eclipse C++ output


Solution

  • That's correct. std::setw does not actually adjust the visible width of a conversion; it adjusts the character count, on the assumption that the output is being viewed with a monospace font (that is, a font in which all characters are the same width).

    So you need to configure Eclipse's console to use a monospaced font. (That will be somewhere in the Preferences menu; hopefully, it's reasonably easy to find.) But note that outside of the printable ascii range, there are lots of characters which don't render to the "constant width" of a monospaced font. Han characters are typically double width, for example, and combining accents and many control characters have width 0. So tabular console output is becoming harder and harder to achieve.

    If you want prettier output and you don't want to go to the trouble of writing a graphical interface -- which is a lot of work if all you want to do is present a table with aligned columns -- your best option might well be to generate HTML and feed the program output through an HTML renderer, such as a web browser.