如何使用printf函数?

发布于 2025-01-02 00:30:53 字数 668 浏览 1 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2025-01-09 00:30:53

你可以

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

假设 name 和 title 是 c 风格的字符串,gross、tax、net 是整数,很可能它们都不是这些类型……但给你一种感觉。 printf 文档提供了打印各种基本类型的指南。

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. the printf documentation gives a guide for printing out the various basic types.

ゝ偶尔ゞ 2025-01-09 00:30:53

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文