在使用 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
请指出我犯了错误的地方。谢谢!
答案 0 :(得分:0)
问题是
stringstream ss(line);
ss >> name >> studentNo>> gender>> indian >> english >> math >> history >> moral;
当您遇到Batman 11 M
这样的3字行时,表达式为ss >> indian
will fail and activate the failbit。这意味着印度人之后的所有变量都不会被修改。就目前而言,它们是未初始化的,它解释了你得到的价值的随机性。
现在要修复此问题,您应该检测特定行中的列数,即number of words in the string,以便您可以将未修改行的状态恢复到其先前状态。
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);
现在使用布尔值来决定是否保存包含或不包含成绩的行