在使用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();
}
为了清楚起见,修改分数的参数是
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
将失败并激活故障位。这意味着印度之后的所有变量都不会被修改。就目前而言,它们是未初始化的,这解释了您获得的值的随机性。
现在要解决此问题,您应该检测特定行中的列数,即字符串中的单词数,以便您可以将未修改的行的状态恢复到其先前状态。
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);
现在使用布尔值来决定是保存带或不带成绩的行