我必须从以下格式的文本文件中获取输入:
Attendance: 5
Midterm: 20
Final: 20
Homework: 15
Projects: 40
Henry, Patrick
Attendance: 12 15
Midterm: 80 100
Homework: 50 100
Homework: 60 100
Homework: 80 100
Project: 90 100
Project: 80 100
Project: 75 100
Final: 80 100
前五行代表该给定类别的权重,后面是学生的姓名。然后,一系列成绩类别及其分数如下。第一个数字代表分数,而第二个数字代表他们可以获得的最大分数。最后一个成绩类别总是最终成绩,其他学生可能会或可能不会成绩。我必须加载这些数据,并根据所有其他等级和权重计算学生的平均成绩。我做了以下事情:
#include "tmp.h"
#include <iostream>
#include <string>
#include <cctype>
#include <fstream> //for reading and writing to/from files
#include <sstream>
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using namespace std;
int main()
{
string command;
cout << "What do you want to do?" << endl;
cin >> command;
ifstream in("grades.txt");
string Attendance, Midterm, Final, Homework, Projects;
int A, M, F, H, P;
in >> Attendance >> A >> Midterm >> M >> Final >> F >> Homework >> H >> Projects >> P;
while(!in.fail())
{
if( command == "Load" || command == "load" || command == "l")
{
string lname, fname;
getline(in, lname, ',');
getline(in.ignore(200,'\n'), fname, '\n');
string cat;
int score, possible;
in >> cat;
double total;
while(cat!="final")
{
double att, mid, home, proj, fin;
in >> score >> possible;
if (cat == "Attendance")
{
att = score/possible*A;
total = total+att;
}
else if (cat == "Midterm")
{
mid = score/possible*M;
total = total+mid;
}
else if (cat == "Homework")
{
home = score/possible*H;
total = total+home;
}
else if (cat == "Project")
{
proj = score/possible*P;
total = total+proj;
}
else
{
fin = score/possible*F;
total = total+fin;
}
cout << total << endl;
in>>cat;
}
}
}
用户被问到他们想要做什么,如果他们输入加载,加载或l,我试图让它读取成绩并输出总数。 (我有一份关于作业要求的其他命令的列表,但那是无关紧要的。我在哪里错了?
谢谢!