[英]data output weird numbers to the file, MAP c++
在使用cin輸入某些值之后,我試圖覆蓋文件中的數據。 我面臨的問題是,應該修改文件的功能實際上並未正確執行。
這是場景,我使用寄存器功能添加了這些數據
碼:
// register a student to the system
void Student::registerStudent(string name, int studentNo, char gender) {
outStream.open("StudentRecord.txt", ios_base::app);
outStream << left << setw(15) << studentNo << setw(15) << gender << right << name << endl;
outStream.close();
}
我將數據添加了3次,我得到了
Batman 11 M
BatGirl 22 F
Joker 33 M
現在出現了問題,我嘗試通過添加其他主題得分來修改蝙蝠俠系列
我想要的結果:
Batman 11 M 100 22 55 22 33
BatGirl 22 F
Joker 33 M
名字后面的數字是科目分數。
但是,當我運行程序以修改特定行時,我得到了
BatGirl 22 F -858993460 -858993460 -858993460 -858993460 -858993460
Batman 11 M 12 86 44 24 55
Joker 33 M -858993460 -858993460 -858993460 -858993460 -858993460
這是特定行修改的代碼
// Function to modify a student's exam scores.
void Student::modifyScore(string newName, 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 >> name >> studentNo>> gender>> indian >> english >> math >> history >> moral;
data[name] = make_tuple(studentNo, gender,indian, english, math, history, moral);
}
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) ;
data[newName] = make_tuple(studentNo, gender, 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(studentNo, gender, newIndian,newEnglish, newMath, newHistory, newMoral) = entry->second;
//int average = averageScore(newIndian,newEnglish, newMath, newHistory, newMoral);
ofs << left << setw(15) << entry->first << setw(15) << studentNo << setw(15) << gender << setw(15) << newIndian << setw(15) << newEnglish << setw(15) << right << newMath << setw(15) << newHistory << setw(15) << newMoral << endl;
}
ofs.close();
}
為了清楚起見, ModifyScore的參數為
newName --> is to find the name in the file
newIndian --> Subject scores
newEnglish --> Subject scores
newMath --> Subject scores
newHistory --> Subject scores
newMoral --> Subject scores
請指出我犯錯的地方。 謝謝!
問題是
stringstream ss(line);
ss >> name >> studentNo>> gender>> indian >> english >> math >> history >> moral;
當遇到像Batman 11 M
這樣的3字線時,表達式ss >> indian
將失敗並激活故障位 。 這意味着indian之后的所有變量都不會被修改。 就目前而言,它們尚未初始化,這說明了您獲得的值的隨機性。
現在要解決此問題,您應該檢測特定行中的列數,即字符串中的單詞數,以便可以將未修改的行的狀態恢復到其先前的狀態。
unsigned int countWordsInString(std::string const& str);
bool fullrecord(std::string const& str)
{
return countWordsInString(str) == 8;
}
map<string, tuple<int, char,int, int, int, int, int, bool> > data; //added bool
stringstream ss(line);
bool hasGrades = fullrecord(line);
if(hasGrades )
ss >> name >> studentNo>> gender>> indian >> english >> math >> history >> moral;
else
ss >> name >> studentNo>> gender;
data[name] = make_tuple(studentNo, gender,indian, english, math, history, moral, hasGrades);
現在使用布爾值來決定是否保存帶有或不帶有成績的行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.