2017-09-14 56 views
-4

我是C++的新手,我有我的第一个任务。我们有一个文本文件,其中包含5个员工姓名,工资和工作时间。如何将项目添加到输出文件?

这是我的代码,以便我的程序可以读取它。

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <string> 
#include <vector> 

using namespace std; 

int main() 
{ 
    ifstream input; 
    ofstream output; 

    string name; 
    int h; 
    float w; 
    int numEployee = 0; 
    double maxPay, minPay, avgPay; 
    int gross, adjGross; 

    //input/output file document path 
    input.open("/Users/jmduller/Documents/xcode/Lab1/Lab1/Employees.txt"); 
    output.open("/Users/jmduller/Documents/xcode/Lab1/Lab1/Employeesoutput.txt"); 

    //checks if the input file is working 
    if (input.fail()) 
    { 
     cout << "Input file failed to open." << endl; 
     system("pause"); 
     exit(0); 
    } 

    //checks if output file is working 
    if (output.fail()) 
    { 
     cout << "Output file failed to open." << endl; 
     system("pause"); 
     exit(0); 
    } 

    //prints the name, wage, hours worked of the employees to the output file 
    while (input >> name >> w >> h) 
    { 
     output << setw(5) << name << setw(5) << w << setw(5) << h << endl; 
    } 


    system("pause"); 
    return 0; 
} 

它正确地阅读它,并给我输出文件,我想要但缺少项目。完整的输出文件应该有员工人数,最高工资,最低工资,平均工资,总工资和调整总额。

任何人都可以帮助我指向正确的方向吗?

感谢

+0

以下是您需要创建[mcve]的说明。只要问问自己以下问题:有人可以拿出你在问题中提供的所有信息,并自己重现你的问题吗?如果不是,那么你的问题不能满足[mcve]的所有要求。 –

+0

好吧,你的'while'循环读取3个标记并将它们传递给输出文件。你为什么期望别的什么东西在那里?你提到**平均工资** - 你在哪里计算它? – Fureeish

+0

您正在努力编写任何信息。你有一个评论,说它有,但你没有添加到该块的代码。如果你这样做,你不会再有问题了。我们没有为你做功课。如果您希望在输出中使用这些项目,请更改代码以包含它们。 –

回答

0

你有什么要做的就是用你的while循环语句中的一些条件和声明(这是从文件中读取)。每次循环执行时增加'numEmployee'变量(计数条目数)。

比较'w'读取以检查它是否低于minPay(初始化为非常大的值)然后更新minPay否则如果高于maxPay(初始化为最小值可能)更新maxPay。

此外,在循环中将'w'添加到另一个变量sumPay(初始化为零),并在最后将其除以numEmployee,即可完成。 只需在return语句之前将它们输出到文件中即可。

相关问题