admin 管理员组文章数量: 1086019
Working on an assignment for my coding class which involves the function below, which is supposed to read quiz scores from an input file and put up to 12 of them into an array. I've narrowed down the issue to specifically when there is an empty line on an input file.
Example of a successful file (each number is its own line):
10
8
7
5
0
Example of a failed file:
10
8
7
5
0
(The last line is empty space.)
The code is as follows.
//Attempting to build an array of up to twelve quiz scores.
int buildQuizArray(int quizArray[], string nameofile){
ifstream infile;
infile.open(nameofile);
int quizzesread = 0;
int input = 0;
std::string line;
int quiznum = 1;
if (infile.fail()){
cout <<"\n"<<nameofile << " did not open\n";
exit(-1);
}
if (infile){
while (quizzesread == 0 || (getline(infile, line) && quizzesread <12)){ //Currently reads one too many times if there's an empty space in a scoring file.
infile >> input;
quizArray[quiznum-1] = input;
quizzesread++;
quiznum++;
test++;
}
}
infile.close();
return quizzesread;
}
I've tried implementing a check for if an empty line is detected using line.empty()
, but all that does is cut off every single test after the first quiz entered into the array. I don't know what I'm missing.
本文标签: cArray copies one too many values if there is an empty line in the input fileStack Overflow
版权声明:本文标题:c++ - Array copies one too many values if there is an empty line in the input file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744076750a2529491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论