2010-12-06 46 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行。壞數據的測試案例也不會傷害。