显示结果 C++ 程序



我需要有关setw()的帮助。我有这个想法:

void PrintRecord(PLAYER &m, ostream &cout)
{
cout << setw(3) << m.PlayerID << 't' << m.LastName << ',' << setw(4)
<< m.FirstName;
if ( m.Hits > 0 || m.Walks > 0 || m.Outs > 0 )
{
cout << "tt" << setprecision(3) << fixed << m.Hits << 't'
<< m.Walks << 't' << m.Outs << 't' << m.BattingAvg
<< 't' << m.OnBaseAvg << endl;
}
else
{
cout << endl;
}
}

这不是在列中打印我的数据。

我想在 Alighn Colmns 上打印我的数据,

谢谢

您应该通过在要打印的每个值之前调用setw来为列中的每个值设置相同的宽度。 通常,使用setw时不需要使用"\t"。 与setprecision不同,setw不是"粘性的",因此您需要在每个打印值之前设置它。

cout << setw(3) << m.PlayerID << setw(20) << m.LastName << ',' << setw(20)
<< m.FirstName;
if ( m.Hits > 0 || m.Walks > 0 || m.Outs > 0 )
{
cout << setw(20) << setprecision(3) << fixed << m.Hits << setw(20)
<< m.Walks << setw(20) << m.Outs << setw(20) << m.BattingAvg
<< setw(20) << m.OnBaseAvg << endl;
}

最新更新