for (int n = 1; n < 6; n++){
cout << "Enter the score for test " << n << endl;
if (n = 1) // n++ gets processed.
cin >> testScore1;
elseif (n = 2) // loop gets stuck here.
cin >> testScore2;
elseif (n = 3)
cin >> testScore3;
elseif (n = 4)
cin >> testScore4;
else
cin >> testScore5;
cout << endl;
}
The first iteration runs, then the second iteration repeats forever.
And why, when I change this part of the program to all if's, does the following code allow the cin's to run in the loop one after the other without allowing the first cout of "enter the score for test " to loop?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for (int n = 1; n < 6; n++){
cout << "Enter the score for test " << n << endl; // This only happens once.
if (n = 1) // n++ happens.
cin >> testScore1;
if (n = 2) // n++
cin >> testScore2;
if (n = 3) // n++
cin >> testScore3;
if (n = 4) // n++
cin >> testScore4;
if (n = 5) // n++
cin >> testScore5;
cout << endl;
}
It is as if the "if" statements are replacing the purpose of the original "for" statement. As soon as it hits an if statement n++ gets processed and the loop is not allowed to continue unless it encounters another if statement or whatever allows the second code set to end.
(n = 1) set n equal to 1 .. thus true being a non zero int, == checks for equality
so this happens each time.
at the end of the block you've n equal to (=) 5... not compared equality
so when then loop end and increment section of the for loop is evauluated it set n to 6
6 is not < 6 so the loop does not perform the 2nd iteration.