PDA

View Full Version : cout problem



mickey
2nd March 2007, 02:55
Hi, I'm printing some values in a loop with this code:


cout << "\tOut" << y << "= " <<output<< " " ;
cout << "\terr = " << error << "\t" << " sq_err = " << _serrv<< " ";
cout << "OK";
cout << "\n";
// I know this can be done in a line...



//output
Out0= 0.9474925 err = 0.05250749 sq_err = 0.001378518 OK
Out0= 0.6843882 err = 0.3156118 sq_err = 0.04980541
Out0= 0.8848782 err = 0.1151218 sq_err = 0.006626516 OK
Out0= 0.7979639 err = 0.2020361 sq_err = 0.0204093 OK
Out0= 0.9102138 err = 0.08978618 sq_err = 0.004030779 OK

Why does any sq_err appear not tabulated (\t seems hans't effect)

wysota
2nd March 2007, 05:11
It has an effect, just the tabulator is one character after the end of the previous text. Look that for "err" rows which are one digit longer "sq_err" fields are tabulated correctly. Either pad (or fill) the err field if it's shorter or calculate the tabulation yourself and add another \t if needed.

jpn
2nd March 2007, 09:27
Maybe you could use setw() instead of tabulators?

Brandybuck
2nd March 2007, 19:12
Tabs will tab to the next tab column, which are eight spaces wide. If you look at your output, you will see that you are off eight spaces. This is a common problem when trying to line up columns with \t characters.