目前我遇到一个大问题,我需要将数据填充到MAP并将其写入文件。
该文件将如下所示:
Name StudentNo Gender Indian English Math History Moral Average
Dragon 33899 M 100 100 100 100 100 100
这是代码
// Function to modify a student's exam scores.
void Student::modifyScore(string newName, int newStudentNo, char newGender, int newIndian, int newEnglish, int newMath, int newHistory, int newMoral) {
map<string, tuple<int, char,int, int, int, int, int> > data;
// Read file and fill data map
ifstream studentRec("StudentRecord.txt");
string line;
while (getline(studentRec, line))
{
string name;
int studentNo;
char gender;
int indian, english, math, history, moral;
stringstream ss(line);
ss >> studentNo>>gender>>name >> indian >> english >> math >> history >> moral;
data[name] = make_tuple(studentNo, gender,indian, english, math, history, moral);
}
studentRec.close();
auto it = data.find(newName) ;
if (it == data.end()) // student not in map, that's an error
return ;
// now it->second holds your student data,
int studentNo = get<0>(data-second) ;
char gender = get<1>(data->second) ;
// Modify data
data[newName] = make_tuple(newStudentNo, newGender ,newIndian,newEnglish, newMath, newHistory, newMoral);
// Open same file for output, overwrite existing data
ofstream ofs("StudentRecord.txt");
for (auto entry = data.begin(); entry != data.end(); ++entry)
{
tie(newStudentNo, newGender,newIndian,newEnglish, newMath, newHistory, newMoral) = entry->second;
int average = averageScore(newIndian,newEnglish, newMath, newHistory, newMoral);
ofs << left << setw(15) << entry->first << setw(15) <<newGender<<newName<< newIndian << setprecision(2) << newEnglish << setw(15) << right << newMath << setw(15) << newHistory << setw(15) << newMoral << average << endl;
}
ofs.close();
}
此代码的问题在于我必须添加 studentNo 和性别作为函数的参数,然后才会覆盖该文件,但我真正想要的是是输入一个名字,然后修改每个主题分数。
示例我真正想要的。
在进行任何修改之前
Name StudentNo Gender Indian English Math History Moral Average
Dragon 33899 M 100 100 100 100 100 100
提示符
Enter the name of the student: // It will find the map that has name Dragon
--> Dragon
Enter the new score for indian subject
--> 77
Enter the new score for English subject
--> 55
Enter the new score for Math subject
--> 100
Enter the new score for History subject
--> 89
Enter the new score for Moral subject
--> 62
修改后
Name StudentNo Gender Indian English Math History Moral Average
Dragon 33899 M 77 55 100 89 62 76.6
正如您所见, studentNo 和性别仍然存在,我无需输入值。唯一改变的是主题分数。我该如何完成这个输出?目前我的代码总是需要输入 studentNo 和性别的输入。
答案 0 :(得分:1)
根据名称,您可以获得该学生的迭代器,然后获取原始数据
auto it = data.find(name) ; // gets current student record from the map
if (it == data.end()) // student not in map, that's an error
return ;
// now it->second holds your student data
// an auto here could be better, but we want to be sure of type
studentNo = get<0>(it->second) ;
gender = get<1>(it->second) ;
答案 1 :(得分:0)
假设您要更新名为&#39; Dragon&#39;的学生记录的StudentNo。你可以这样做:
int NewStudentNo;
string NewName="Dragon";
auto it = data.find(NewName) ; // gets current student record from the map
if (it == data.end()) // student not in map, that's an error
return ;
std::cout<<"Enter new student Number";
std::cin>>NewStudentNo;
std::get<0>(it->second) = NewStudentNo;