C++習題二第12題

輸入職工信息,打印到文件,而後讀取文件再輸出職工實發工資ios

#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與大語句分開,不然會輸出兩次最後一行的信息app

相關文章
相關標籤/搜索