在C ++中使用printf制作表 [英] Making a table with printf in c++

查看:127
本文介绍了在C ++中使用printf制作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图制作一张这样的桌子。...(只是没有用来分隔每个项目的圆点)

I'm trying to make a table like this.... (Just without the dots I used to separate the each item)


每周薪水:

Weekly Payroll:

姓名.........标题...。毛......税......净

Name.....................Title.......Gross......Tax......Net

Ebenezer Scrooge Partner 250.00 ..62.25 .187.75

Ebenezer Scrooge Partner 250.00 ..62.25 .187.75

鲍勃·克拉奇特..........文员... 15.00 .... 2.00 ..13.00

Bob Cratchit ..........Clerk ......15.00 ....2.00 ..13.00

这是我这部分的代码...。

This is what my code looks like for this part....

for (int count=0; count < numberOfEmployees; count++)
{
    cout << "Employee: \n";
    cout << "\t Name: ";
    cin.getline (employees[count].name, EMPLOYEESIZE); 

    cout << "\t Title: ";
    cin.getline (employees[count].title, EMPLOYEESIZE);

    cout << "\t SSNum: ";
    cin >> employees[count].SSNum;

    cout << "\t Salary: ";
    cin >> employees[count].Salary;

    cout << "\t Withholding Exemptions: ";
    cin >> employees[count].Withholding_Exemptions; 
    cin.ignore();

    cout << "\n";
}


double gross;
double tax;
double net;
double adjusted_income;

cout << "\n";

cout << "Weekly Payroll: \nName \t \t Title \t Gross \t Tax \t Net \n";


for (int count=0; count < numberOfEmployees; count++)
{
    gross = employees[count].Salary;
    adjusted_income = subtraction_times (employees[count].Salary, employees[count].Withholding_Exemptions);
    tax = adjusted_income * .25;
    net = subtraction (gross, tax);

    printf ("\n%s", employees[count].name); 
}

我有表的第一部分(名称部分),但是我现在不知道要做桌子的其余部分。有人可以帮我吗?

I have the first part of the table(the name part), but after that I dont know now to do the rest of the table. Can anyone help me?

谢谢

推荐答案

您可以使用 printf ,带有左对齐标志(-)。

You can use printf with left-justify flag (-).

printf("%-10s", "title"); // this will left-align "title" in space of 10 characters

这是一个示例程序:

#include <string>
using namespace std;

int main()
{
    string name = "Bob Cratchit";
    string title = "Clerk";
    float gross = 15;
    float tax = 2;
    float net = 13;

    printf("%-25s%-20s%-10s%-10s%-10s\n", "Name", "Title", "Gross", "Tax", "Net"); 
    printf("%-25s%-20s%-10.2f%-10.2f%-10.2f\n", name.c_str(), title.c_str(), gross, tax, net); 
    return 0;
}

输出:

Name                     Title               Gross     Tax       Net
Bob Cratchit             Clerk               15.00     2.00      13.00

这篇关于在C ++中使用printf制作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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