Reputation: 25
I'm still learning the fundamentals of C# and I'm currently busy with some code which has got me stuck in an infinite loop.
do
{
Console.Write("Please enter the Exam mark for student " + (index + 1) + ": ");
Exam[index] = double.Parse(Console.ReadLine());
Console.Write("Please enter the DP for student " + (index + 1) + ": ");
DP[index] = double.Parse(Console.ReadLine());
Final[index] = (DP[index] + Exam[index]) / 2;
index++;
} while (DP[index] != 999);
The code doesn't break when I type 999.
Upvotes: 2
Views: 1102
Reputation: 156898
You increase the index with 1 just before the while
condition is checked. I guess the value is 0
on that index...
A solution can be to remember the previous value in a variable, or subtract one from the index:
} while (DP[index - 1] != 999);
Another problem might be the use of a double, since it is imprecise, it doesn't always match to 999
if you entered that value. If you can, use int
or decimal
.
Upvotes: 4