简体   繁体   English

如何使用printf功能?

[英]How to use the printf function?

I have a quick question about the printf function in c++. 我对C ++中的printf函数有一个快速的问题。

How do I put this for loop onto the screen with the printf function? 如何使用printf函数将此for循环显示在屏幕上?

for (int count = 0; count < numberOfEmployees; count++) {
    cout << employees[count].name << " \t" << employees[count].title << " \t" <<
                                                gross << "\t" << tax << "\t" << net << "\n";
}

So, I need to take out the cout and put in a call to printf . 因此,我需要取出cout并调用printf

It has to look like this, but without the dots: 它必须看起来像这样,但没有点:

Weekly Payroll: 每周薪资:

Name .. Title ... Gross... Tax... Net 名称..职务...毛额...税额...净额

Ebenezer .... Partner.... 250.00...... 62.25 .... 187.75 Ebenezer ....合作伙伴.... 250.00 ...... 62.25 .... 187.75

Bob Cratchit Clerk ...... 15.00 ......... 2.00 ...... 13.00 鲍勃·克拉奇蒂特(Bob Cratchit)秘书... ...... 15.00 ......... 2.00 ...... 13.00

you can do 你可以做

printf("%s \t%s \t%d\t%d\t%d\n",
            employees[count].name, 
            employees[count].title, 
            gross, tax, net);

assuming name and title are c style strings, and gross, tax,net are ints, most likely none of them are those types....but gives you the feel for it. 假设名称和标题是c样式的字符串,而gross,tax,net是整数,则很可能它们都不是这些类型。 the printf documentation gives a guide for printing out the various basic types. printf文档提供了有关打印各种基本类型的指南。

The idea w/ printf is that you have a "format string" with placeholders to be filled in by supplied values. 带printf的想法是,您有一个“格式字符串”,其中的占位符将由提供的值填充。 In your case, the format string might look like "%s \\t%s \\t%f\\t%f\\t%f\\n", where each placeholder starts w/ a % followed by a specification of how to interpret the supplied value (for example, s means a string and f means a float). 在您的情况下,格式字符串可能看起来像“%s \\ t%s \\ t%f \\ t%f \\ t%f \\ n”,其中每个占位符均以%开头,后跟有关如何解释提供的值(例如,s表示字符串,f表示浮点数)。 There are a lot more options than that (things like field sizes, # of decimal places, left-or-right alignment), but that's the basic idea. 除此之外,还有更多选择(诸如字段大小,小数位数,左右对齐),但这是基本思想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM