简体   繁体   English

在输出中使用 printf 的最佳方法是什么?

[英]What is the best way to use printf in output?

This is code for a project I have recently almost completed.这是我最近几乎完成的一个项目的代码。 The code takes a gross value and then does some math to get a bunch of new values.代码取一个总值,然后做一些数学运算来获得一堆新值。 My final instruction is to output the console output into a file.我的最后一条指令是将控制台输出输出到文件中。 I need both a console output and a file output.我需要控制台输出和文件输出。 My issue is I have been trying for almost a day to figure out how to output into a file with printf.我的问题是我已经尝试了将近一天来弄清楚如何使用 printf 输出到文件中。 If anyone has a solution it would be much appreciated.如果有人有解决方案,将不胜感激。

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string FirstName; // declaring variables
    string LastName;
    double gross;               
    double federalTax;
    double stateTax;            // each variable will be holding the total taxed amount for each category
    double ssTax;               // example if stateTax is 3.5%, then the stateTax variable will be 3.5% of gross
    double mmTax;
    double pensionPlan;
    double healthIns;
    double netDeduct;
    double netPay;

    cout << "Please enter your first name: "; // prompting for user input and storing in variables
    cin >> FirstName;
    cout << "\nPlease enter your last name: ";
    cin >> LastName;
    cout << "\nPlease enter your gross paycheck amount: ";
    cin >> gross;

    federalTax = gross * .15;       // the math of the taxing
    stateTax = gross * .035;
    ssTax = gross * .0575;
    mmTax = gross * .0275;
    pensionPlan = gross * .05;
    healthIns = 75;
    netDeduct = federalTax + stateTax + ssTax + mmTax + pensionPlan + healthIns;
    netPay = gross - netDeduct;

    
    cout << endl,cout << FirstName + " " + LastName; cout << endl;       // printing of the results
    printf("Gross Amount: %............ $%7.2f ", gross); cout << endl;
    printf("Federal Tax: %............. $%7.2f ", federalTax); cout << endl;
    printf("State Tax: %............... $%7.2f ", stateTax); cout << endl;
    printf("Social Security Tax: %..... $%7.2f ", ssTax); cout << endl;
    printf("Medicare/Medicaid Tax: %... $%7.2f ", mmTax); cout << endl;
    printf("Pension Plan: %............ $%7.2f ", pensionPlan); cout << endl;
    printf("Health Insurance: %........ $%7.2f ", healthIns); cout << endl;
    printf("Net Pay: %................. $%7.2f ", netPay); cout << endl;

}

Side note: If you are coding in C++ then you should use the newer functionalities such as cout or fstream instead of printf of fprintf旁注:如果您使用 C++ 进行编码,那么您应该使用较新的功能,例如 cout 或 fstream,而不是 fprintf 的 printf

Now, coming to your question: What you are probably looking for is fprintf()现在,来回答您的问题:您可能正在寻找的是fprintf()
Here's a snippet using the function that I just mentioned above:这是一个使用我刚才提到的函数的片段:

   FILE * fp = fopen ("file.txt", "w+");
   fprintf(fp, "%s %f", "Your net pay is: ", netPay);

As you can see, it's quite similar to printf and that's why I think it should be self explanatory to you.如您所见,它与 printf 非常相似,这就是为什么我认为它应该对您一目了然。 The only big difference is passing a FILE pointer to it唯一的大区别是将 FILE 指针传递给它
Check these links out to learn more about fprintf() and fopen()查看这些链接以了解有关fprintf()fopen()的更多信息

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

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