如何使用printf函数?
我有一个关于 C++ 中的 printf
函数的快速问题。
如何使用 printf 函数将此 for 循环放到屏幕上?
for (int count = 0; count < numberOfEmployees; count++) {
cout << employees[count].name << " \t" << employees[count].title << " \t" <<
gross << "\t" << tax << "\t" << net << "\n";
}
因此,我需要取出 cout
并调用 printf
。
它必须看起来像这样,但没有点:
每周工资单:
姓名 .. 标题 ... 总... 税... Net
Ebenezer .... 合作伙伴 .... 250.00...... 62.25 。 ... 187.75
鲍勃·克拉奇特 职员 ...... 15.00 ...... 2.00 ...... 13.00
I have a quick question about the printf
function in c++.
How do I put this for loop onto the screen with the printf
function?
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
.
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
Bob Cratchit Clerk ...... 15.00 ......... 2.00 ...... 13.00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
你可以
假设 name 和 title 是 c 风格的字符串,gross、tax、net 是整数,很可能它们都不是这些类型……但给你一种感觉。 printf 文档提供了打印各种基本类型的指南。
you can do
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. the printf documentation gives a guide for printing out the various basic types.
printf 的想法是,您有一个“格式字符串”,其中包含要由提供的值填充的占位符。在您的情况下,格式字符串可能看起来像“%s \t%s \t%f\t%f\t%f\n”,其中每个占位符以 % 开头,后跟如何解释的规范提供的值(例如,s 表示字符串,f 表示浮点数)。还有更多的选项(例如字段大小、小数位数、左对齐或右对齐),但这就是基本思想。
The idea w/ printf is that you have a "format string" with placeholders to be filled in by supplied values. 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). 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.