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