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