新代碼時,排隊的事情了....如何輸出到C++中的文件
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void gradeg (double & average, string & grade)
{
if (average >= 90)
{
grade = "A";
}
else if ((average < 90) & (average >= 80))
{
grade = "B";
}
else if ((average < 80) & (average >= 70))
{
grade = "C";
}
else if ((average < 70) & (average >= 60))
{
grade = "D";
}
else
{
grade = "F";
}
}
void totalg (int & total, int & s1, int & s2, int & s3)
{
total=(s1+s2+s3);
}
void averageg (double & average, int & total)
{
average=total/3.0;
}
int main()
{
ifstream fin;
ofstream fout;
fout.setf (ios::fixed);
fout.setf (ios::showpoint);
fout.precision (2);
char filename[15];
fin.open("input.txt");
cout << "Please enter the name of your output file: ";
cin >> filename;
fout.open(filename);
if (fin.fail())
{
cout << "Failed to open INPUT file." << endl;
exit(1);
}
if (fout.fail())
{
cout << "Failed to open OUTPUT file." << endl;
exit(1);
}
string grade, name;
int s1, s2, s3, total=0;
double average=0.0;
fout << left << "Names" << setw(24) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade \n";
cout << "Reading from input files.....";
while (! fin.eof())
{
fin >> name >> s1 >> s2 >> s3;
totalg (total, s1, s2, s3);
averageg (average, total);
gradeg (average, grade);
fout << left << name << setw(24) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n";
}
cout << "Your output file has been created and the computation results have been stored.";
return 0;
}
新的輸出...
NamesScore 1 Score 2 Score 3 Total Average Grade
DAN100 70 85 255 85.00 B
JANE78 82 90 250 83.33 B
PETER82 84 91 257 85.67 B
MINIE98 100 75 273 91.00 A
JOSEPH71 62 100 233 77.67 C
CHRISTOPHER91 75 82 248 82.67 B
BEN54 84 77 215 71.67 C
好吧,我更新了我的代碼並輸出了...仍然有問題...任何想法? – Zud 2010-10-11 01:08:54
你沒有給名稱 – 2010-10-11 01:11:09
稱呼setw非常感謝。 – Zud 2010-10-11 01:13:30