新代码......
#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
答案 0 :(得分:3)
使用“左”操纵器作为名称,然后使用数字的“右”操纵器:
fout << left << setw(19) << "Names" << right << setw(10) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade" << '\n';
fout << left << setw(19) << name << right << setw(10) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n";
这是我得到的输出:
Names Score 1 Score 2 Score 3 Total Average Grade
DAN 100 70 85 255 85.00 B
JANE 78 82 90 250 83.33 B
PETER 82 84 91 257 85.67 B
MINIE 98 100 75 273 91.00 A
JOSEPH 71 62 100 233 77.67 C
CHRISTOPHER 91 75 82 248 82.67 B
BEN 54 84 77 215 71.67 C
BEN 54 84 77 215 71.67 C
答案 1 :(得分:3)
你犯的是一个非常简单的错误,即格式化操作符(例如setw
)必须在之前它格式化的数据,如下所示:
fout << left << setw(24) << "Names" << setw(10) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade \n";
fout << left << setw(24) << name << setw(10) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n";
// ^^^^^^^^^^^ NOTE THIS PART
单词Names
将占用5个字符,后面会有19个空格。接下来,您将看到Score 1
(7个字符),后跟3个空格,依此类推。
Names Score 1 Score 2
Ken Bloom 75 100
Alec 100 75