输出对齐的列 [英] Output aligned columns

查看:69
本文介绍了输出对齐的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++。我在格式化程序输出时遇到问题。我想在此打印完全对齐的列,但到目前为止我做不到,这是我的代码:

I am learning C++. I have a problem formatting the output of my program. I would like to print there columns perfectly aligned but so far I cannot do it, here is my code:

int main()
{
    employee employees[5];

    employees[0].setEmployee("Stone", 35.75, 053);
    employees[1].setEmployee("Rubble", 12, 163);
    employees[2].setEmployee("Flintstone", 15.75, 97);
    employees[3].setEmployee("Pebble", 10.25, 104);
    employees[4].setEmployee("Rockwall", 22.75, 15);

    printEmployees(employees, 5);

    return 0;
}

// print the employees in my array
void printEmployees(employee employees[], int number)
{
    int i;

    for (i=0; i<number; i++) {
        employees[i].printEmployee();// this is the method that give me problems
    }
    cout << "\n";
}

在班级员工中,我有打印员工方法:

in the class employee I have the print employee method:

void printEmployee() const
{
    cout << fixed;
    cout << surname << setw(10) << empNumber << "\t" << setw(4) << hourlyRate << "\n";
}

问题是当我打印 flinstones行时,emp编号和费率不排列整齐。发生这样的事情:

Problem is when I print "flinstones" line the emp number and rate are not lined up. something like this happens:


Stone        43 35.750000
Rubble       163    12.000000
Flintstone        97    15.750000
Pebble       104    10.250000
Rockwall        15  22.750000

有人可以帮助我吗? (我尝试添加选项卡..但没有帮助)

Can anybody help me? (I tried to add tabs.. but it didn't help)

推荐答案

在print employee方法的class employee中:
使用此行进行打印。

In the class employee of print employee method: Use this line to print.

cout << setw(20) << left << surname << setw(10) << left << empNumber << setw(4) << hourlyRate << endl;

您忘记添加 <<左

希望它会有用。

这篇关于输出对齐的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆