On Thursday, 20 August 2020 at 19:13:31 UTC, Ali Çehreli wrote:
On 8/19/20 11:46 AM, Flade wrote:
[...]
In some cases clearerr() and readln() may be what is needed:
import std.stdio;
void main() {
int x;
bool accepted = false;
while (!accepted) {
try {
write("x: ");
readf(" %d", x);
accepted = true;
} catch (Exception msg) {
writeln("Please give a right coordinate");
stdin.clearerr();
stdin.readln();
}
}
writeln("x is ", x);
}
Note that I used " %d" because "%d/n" would not clean when
nothing was input by just hitting the Enter key. A space
character in the format string means "read and ignore any
whitespace at this point" and I like it. :)
Also note I changed the name of the variable as 'accepted'. :)
Ali
Thanks a lot! Very useful info!!! And sorry for answering so
late, I wasn't expecting someone to answer so I wasn't checking.
Have a great day my friend!