2010-12-06 10 views
1

Ich brauche ein wenig Programmierhilfe für die Klasse. Ich habe den folgenden Code geschrieben, aber mein Lehrer sagt, dass ich beim Lesen der Datendatei 2 For-Schleifen verwenden muss. Ich bin nicht sicher, wie das geht ... irgendwelche Vorschläge?Schalten Sie 1 C++ Schleife in 2

Mein Code:

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

Ihr Lehrer möchte wahrscheinlich, dass Sie zwei verschachtelte Schleifen verwenden, anstatt von Hand von 0 bis 7 zu gehen. – thkala

+0

kannst du bitte dein Beispiel für 100 Kandidaten posten? (mit 'contestantNames [100] [100]') – ruslik

Antwort

3

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

Schreiben Sie schrieb eine for Schleife.

Wo gibt es sonst Wiederholung?

2

Ihre zwei for-Schleifen wäre

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

Ihr Lehrer Sie, dass die durchgehende Linie von judgeScores in eine for-Schleife vereinfachen wollen, die viel einfacher und leichter zu lesen ist.

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

Ich denke, Ihr Lehrer Schleife bedeutet, einmal über alle Teilnehmer und Schleife wieder zur Eingabe der Informationen jeden Teilnehmers

So etwas wie dies versucht (Namen und Punktzahl.):

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

Hinweis: Die Klammern sind nicht erforderlich, da jede Anweisung aus einer Zeile besteht. Dies ist die Ausnahme von der Regel - wenn es mehr als 1 Anweisung in der for Schleife gibt, wären Klammern erforderlich. (Wie Sie vielleicht wissen.)

0

Wahrscheinlich möchten Sie, dass Sie den Fall berücksichtigen, wenn mehr als zwei Kandidaten vorhanden sind. Etwas nach dem Vorbild von

nc = 6; // the number of contenstans 
for (int c=0; c<nc; c++) { 
    // other for loop here... 
} 
0

Surprise Sie Lehrer mit sogar drei Schleifen:

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

Irgendein Vorschlag, der mit "überrasche deinen Lehrer" beginnt, sollte wahrscheinlich sofort rote Fahnen schicken. –

+0

@Cody Grey: Warum nicht? Ich meinte es gut. Wie Sie sehen können, ist diese Version die einzige, die eine einfachere Änderung der Anzahl der "Kandidaten" erlaubt. Wenn ich der Lehrer wäre, hätte ich diese Version gerne gesehen. – detunized

0

eine while-Schleife Put für das Ende der Datei (EOF) bei Überprüfung gibt es mehr als 6 Zeilen. Testfälle für schlechte Daten würden auch nicht schaden.