2010-12-06 50 views
1

我需要一些编程帮助。我写了下面的代码,但是我的老师说在读数据文件时我需要使用2 for循环。我不知道该怎么做......有什么建议吗?将1个C++循环转换为2个

我的代码:

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(); 
+1

你的老师可能希望你使用两个嵌套循环,而不是手动从0到7。 – thkala 2010-12-06 04:02:06

+0

你可以请你提供100个参赛者的例子吗? (with`contestantNames [100] [100]`) – ruslik 2010-12-06 04:02:42

回答

3

而不是repetitiously写

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循环。

还有哪里有重复?

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]; 
    } 
} 

你的教练要你judgeScores的是不断线简化成一个for循环是更简单,更易于阅读。

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(); 
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循环中有多条语句,则需要括号。 (你可能知道)

0

你的老师可能希望你考虑的情况下,当有超过两个的内容。沿着

nc = 6; // the number of contenstans 
for (int c=0; c<nc; c++) { 
    // other for loop here... 
} 
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(); 
0

把while循环的文件(EOF)的情况下,最终检查有超过6行。坏数据的测试案例也不会伤害。