输入职工信息,打印到文件,然后读取文件再输出职工实发工资

#include<iostream>
#include<fstream>
#include<iomanip>


using namespace std;

int main(){
	string number;
	string name;
	int basic_salary;
	int overtime_salary;
	int bonus_salary;
	int deduction;

	ofstream outData("Salary.dat");
	ifstream inData;

	outData<<"职工编号  "<<"姓名      "<<"基本工资  "<<"加班工资  "<<"奖金      "<<"扣除      "<<"实发工资  "<<endl;
	for(int i=0;i<3;i++){
		cout<<"职工编号: "; cin>>number;
		cout<<"姓名    : "; cin>>name;
		cout<<"基本工资: "; cin>>basic_salary;
		cout<<"加班工资: "; cin>>overtime_salary;
		cout<<"奖金    : "; cin>>bonus_salary;
		cout<<"扣除工资: "; cin>>deduction;
		outData<<setiosflags(ios::left);
		outData<<setw(10)<<number<<setw(10)<<name<<setw(10)<<basic_salary<<setw(10)<<overtime_salary<<setw(10)<<bonus_salary<<setw(10)<<deduction<<endl;
	}
	outData.close();

	inData.open("Salary.dat",ios::app);

	string str[7];
	int real_salary;
	for(int i=0;i<7;i++)
		inData>>str[i];
	inData>>number;
	while(!inData.eof()){
		inData>>name>>basic_salary>>overtime_salary>>bonus_salary>>deduction;
		real_salary=basic_salary+overtime_salary+bonus_salary-deduction;

		cout<<name<<"'s salary is "<<real_salary<<endl;
		inData>>number;
	}

	inData.close();

	return 0;
}

再读取文件时,注意细节,把inData>>number与大语句分开,否则会输出两次最后一行的信息