我需要为课程提供一些编程帮助。我在下面编写了代码,但是我的老师说我在阅读数据文件时需要使用2 for循环。我不知道怎么做...有什么建议吗?
我的代码:
ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++)
infile >> contestantNames[i][0] >> contestantNames[i][1] >> judgeScores[i][0] >> judgeScores[i][1] >> judgeScores[i][2] >> judgeScores[i][3] >> judgeScores[i][4] >> judgeScores[i][5] >> judgeScores[i][6] >> judgeScores[i][7];
infile.close();
答案 0 :(得分:3)
而不是重复写作
infile >> contestantNames[0][0] >> contestantNames[0][1] >> judgeScores[0][0] >> judgeScores[0][1] >> judgeScores[0][2] >> judgeScores[0][3] >> judgeScores[0][4] >> judgeScores[0][5] >> judgeScores[0][6] >> judgeScores[0][7];
infile >> contestantNames[1][0] >> contestantNames[1][1] >> judgeScores[1][0] >> judgeScores[1][1] >> judgeScores[1][2] >> judgeScores[1][3] >> judgeScores[1][4] >> judgeScores[1][5] >> judgeScores[1][6] >> judgeScores[1][7];
infile >> contestantNames[2][0] >> contestantNames[2][1] >> judgeScores[2][0] >> judgeScores[2][1] >> judgeScores[2][2] >> judgeScores[2][3] >> judgeScores[2][4] >> judgeScores[2][5] >> judgeScores[2][6] >> judgeScores[2][7];
infile >> contestantNames[3][0] >> contestantNames[3][1] >> judgeScores[3][0] >> judgeScores[3][1] >> judgeScores[3][2] >> judgeScores[3][3] >> judgeScores[3][4] >> judgeScores[3][5] >> judgeScores[3][6] >> judgeScores[3][7];
infile >> contestantNames[4][0] >> contestantNames[4][1] >> judgeScores[4][0] >> judgeScores[4][1] >> judgeScores[4][2] >> judgeScores[4][3] >> judgeScores[4][4] >> judgeScores[4][5] >> judgeScores[4][6] >> judgeScores[4][7];
infile >> contestantNames[5][0] >> contestantNames[5][1] >> judgeScores[5][0] >> judgeScores[5][1] >> judgeScores[5][2] >> judgeScores[5][3] >> judgeScores[5][4] >> judgeScores[5][5] >> judgeScores[5][6] >> judgeScores[5][7];
infile >> contestantNames[6][0] >> contestantNames[6][1] >> judgeScores[6][0] >> judgeScores[6][1] >> judgeScores[6][2] >> judgeScores[6][3] >> judgeScores[6][4] >> judgeScores[6][5] >> judgeScores[6][6] >> judgeScores[6][7];
你写了一个for
循环。
还有什么重复吗?
答案 1 :(得分:2)
你的两个for循环将是
for (int i = 0; i <= 6; i++) {
infile >> contestantNames[i][0] >> contestantNames[i][1];
for (int j = 0; j <= 7; j++) {
infile >> judgeScores[i][j];
}
}
您的教师希望您将连续的judScores简化为for循环,这样更容易阅读。
答案 2 :(得分:1)
ifstream infile;
infile.open("dive.txt");
for (int i = 0; i <= 6; i++)
{
infile >> contestantNames[i][0] >> contestantNames[i][1];
// The next part is what your teacher was talking about.
// You wrote judgeScores[i][0] >> judgeScores[i][1] >> ...
// seven times, which is pretty redundant. Programmers are *extremely* lazy
// so we loop constantly, wherever possible:
for (int j = 0; j <= 7; j++)
{
infile >> judgeScores[i][j];
}
}
infile.close();
答案 3 :(得分:0)
我认为你的教练意味着在所有参赛者中循环一次,然后再次循环输入每个参赛者的信息(姓名和分数)。
所以尝试这样的事情:
ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++) {
infile >> contestantNames[i][0] >> contestantNames[i][1];
for (int j = 0; i <= 7; j++) {
infile >> judgeScores[i][j];
}
}
infile.close();
注意:括号不是必需的,因为每个语句都包含一行。但这是规则的例外 - 如果for
循环中有多于1个语句,则需要括号 。 (你可能知道。)
答案 4 :(得分:0)
当有两个以上的居民时,你老师可能会要你考虑这个案子。
的内容nc = 6; // the number of contenstans
for (int c=0; c<nc; c++) {
// other for loop here...
}
答案 5 :(得分:0)
即使是三个循环也让老师惊喜:
ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++)
{
for (int j = 0; j < 2; ++j)
{
infile >> contestantNames[i][j];
}
for (int j = 0; j < 8; ++j)
{
infile >> judgeScores[i][j];
}
}
infile.close();
答案 6 :(得分:0)
使用while循环检查文件结尾(EOF),以防有超过6行。坏数据的测试用例也不会受到影响。