1
我正在尝试将特定变量写入文件,但奇怪的是,在我将其写入文件后,其中的2个变量没有写入文件。变量是平均值和等级。某些变量不会写入C++中的文件
下面的代码:
// Function to modify a student's exam scores.
void ArtStudent::modifyScore(string newName, int newIndian, int newEnglish, int newMath, int newHistory, int newMoral, int newEcon, int newCommerce, int newArt) {
map<string, tuple<int, char,int, int, int, int, int, int , int, int> > data;
// Read file and fill data map
ifstream studentRec("ArtStudentRecord.txt");
string line;
while (getline(studentRec, line))
{
string name;
int studentNo;
char gender;
int indian = 0;
int english = 0;
int math = 0;
int history = 0;
int moral = 0;
int economic = 0;
int commerce = 0;
int art = 0;
stringstream ss(line);
ss >> name >> studentNo >> gender >> indian >> english >> math >> history >> moral >> economic >> commerce >> art ;
data[name] = make_tuple(studentNo, gender, indian, english, math, history, moral, economic, commerce, art);
}
studentRec.close();
// Modify data
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 ;
// now it->second holds your student data
// an auto here could be better, but we want to be sure of type
auto studentNo = get<0>(it->second) ;
auto gender = get<1>(it->second) ;
// Modify Data
data[newName] = make_tuple(studentNo, gender, newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt);
// Open same file for output, overwrite existing data
ofstream ofs("ArtStudentRecord.txt");
for (auto entry = data.begin(); entry != data.end(); ++entry)
{
tie(studentNo, gender, newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt) = entry->second;
double average = averageScore(newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt);
char grade = getGrade(newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt, average);
ofs << left << setw(15) << entry->first << setw(15) << studentNo << setw(15) << gender << setw(15) << newIndian << setw(15) << newEnglish << setw(15) << newMath << setw(15) << newHistory << setw(15) << newMoral << setw(15) <<
newEcon << setw(15) << newCommerce << setw(15) << newArt << setw(15) << right << average << grade << endl;
}
ofs.close();
}
只有两个变量没有得到写入文件,平均和性别
为了清楚起见,我有两个这些函数执行一些算法来计算平均分数并根据平均分数得出分数。
计算平均得分功能
double ArtStudent::averageScore(int malay, int english, int math, int history, int moral, int econ, int commerce, int art) {
double result = (malay + english + math + history + moral + econ + commerce + art)/8;
return result;
}
获取学生的年级功能
// get Grade for student
char ArtStudent::getGrade(int malay, int english, int math, int history, int moral, int econ, int commerce, int art, double avScore) {
if (malay < 60 || english < 60 || math < 60 || history < 60 || moral < 60 || econ < 60 || commerce < 60 || art < 60) {
return 'F';
}
if (avScore < 60) {
return 'F';
} else if (avScore > 59 && avScore < 70){
return 'D';
} else if (avScore > 69 && avScore < 80) {
return 'C';
} else if (avScore > 79 && avScore < 90) {
return 'B';
} else {
return 'A';
}
}
我运行的功能后,我总是会得到这样的结果
Batman 316536 M 33 99 22 31 44 44 55 12
正如你所看到的,它会写11个变量,而它应该是13个变量
谢谢。
确定吗?因为它仍然没有显示在记事本 – airsoftFreak
我使用记事本++,它显示了我。尝试其他编辑器。(虽然它也出现在普通的记事本中) – aslg
我尝试了崇高的文字,但它仍然没有出现。 – airsoftFreak