閱讀我怎麼可以打印此到一個輸出文件,並通過周和總體周4周計算這些人的工資總和與平均值...輸入輸出C++從txt文件
例如TXT文件。 。
doe jane
williams tom
lons adams
45.7 56.3 345.6 344.7 // week 1
43.6 89.0 543.6 12.5 // week 1 person 2
90.5 78.0 345.4 345.6 //week 1 person 3
67.5 34.5 56.6 34.5 // week2 person 1
etc....for 4 weeks..
我知道有通過使用循環更簡單的方法,我可以得到一些幫助,謝謝:)
這是我迄今爲止
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
double s1, s2, s3 , s4 ,s5;
double t1, t2,t3,t4,t5;
double w1, w2,w3,w4,w5;
string personlast,personfirst,personlast2,personfirst2,personlast3,personfirst3;
double sum, average;
int numberpeople, numberofweeks;
infile.open("data.txt");
outfile.open("output.txt");
outfile<< fixed<< showpoint;
outfile<< setprecision(2);
infile>> numberpeople >> personlast >> personfirst >> personlast2 >> personfirst2>>
personlast3 >> personfirst3 >> numberofweeks;
outfile<< " The number of salespeople are " << numberpeople <<"they are" <<
personlast << personfirst << "and " <<
personlast2 << personfirst2 <<
"and " << personlast3 << personfirst3 <<"Number of weeks = " << numberofweeks;
infile>> s1 >> s2 >> s3 >> s4 >> s5;
outfile <<" sales for week 1 "<< " for" << personlast << personfirst << s1 << s2
<< s3 << s4 << s5 << endl;
sum= s1+s2+s3+s4+s5;
outfile <<"Sum of first week is " << sum<<endl;
infile >> t1 >> t2 >> t3 >> t4 >> t5;
outfile <<" sales for week 1 "<< " for" << personlast2 << personfirst2 << t1 << t2
<< t3 << t4 << t5 <<endl;
infile.close();
outfile.close();
return 0;
它可能有助於說明行內每個數字代表什麼。 – jma127
爲什麼不使用'std :: vector'-s?用'for'循環? –
@BasileStarynkevitch 20代表說它是爲C++類,你不能使用'std :: vector' – evanmcdonnal