Step 2: read the answers
Now the quiz asks each question and waits for an answer. It needs somewhere to put the answer. In
.NET, Console.ReadLine() returns a new string each time. Here that string would never be freed, and
a long-running program would grow with every line it read. Instead, the program reads into a buffer
on the stack, which it reuses for every answer:
// Each answer is read into this; nothing is allocated per question.
Span<char> buffer = stackalloc char[32];
int right = 0, asked = 0;
Ask and wait
// In.ReadLine flushes Out first, so the prompt shows before the
// program waits for the answer.
Out.Write($"Q{i}: {a} + {b} = ");
int length = In.ReadLine(buffer);
if (length < 0)
{
Out.Line();
Out.Line("(end of input)");
break;
}
asked++;
Out.Write writes the prompt without ending the line. Out sends its buffer to the terminal only when
a line ends, so a prompt would normally sit in the buffer while the program waits. In.ReadLine
flushes Out before it reads, so the prompt shows.
In.ReadLine(buffer) reads one line into the buffer and returns its length. The line ending (\n or
\r\n) isn't included, and the input is decoded as UTF-8. At the end of input it returns -1: when a
file of answers runs out, or when you press Ctrl-D at a terminal.
Check the answer
ReadOnlySpan<char> answer = buffer.Slice(0, Math.Min(length, buffer.Length));
if (Str.TryParse(answer.Trim(), out long value) && value == a + b)
{
right++;
Out.Line("right");
}
else
{
Out.Line($"no, it's {a + b}");
}
buffer.Slice(0, length) is the answer: a span over the buffer, not a copy. Trim() returns a
narrower span, and Str.TryParse reads a number from it. None of this allocates. The span is only
valid until the next In.ReadLine overwrites the buffer, which is fine here: the answer is checked and
forgotten before the next question.
Math.Min guards against a line longer than the buffer. When that happens, In.ReadLine returns the
line's full length, and only its start is in the buffer. Step 3 handles it properly.
Run it
Type the answers, or give a file of them:
./build.sh QuizStep2
printf '46\n5\nseventeen\n45\n' | ./run.sh QuizStep2
Q1: 38 + 8 = right
Q2: 5 + 0 = right
Q3: 12 + 5 = no, it's 17
Q4: 2 + 43 = right
Q5: 27 + 8 =
(end of input)
3 of 4 right
With answers from a file, they aren't echoed, so each result follows its question on the same line. At a terminal, you see what you type.
Next: handle bad input.