So I've been trying to get input in D and when I check to make sure that the input is correct, I either end up with an infinite loop or the input buffer overflowing.
import std.stdio;
import std.string;
void main(){
char[] data;
writeln("Hey Player ",1,". Would you like to make a guess?\n Type 'y' to guess or 'n' to continue ");
write("> ");
readf(" %s", &data);
char guessY = data[0];
while(guessY != 'y' && guessY != 'Y' && guessY != 'n' && guessY != 'N')
{
writeln(guessY, " ",data);
writeln("");
writeln("Hey Player ",1," enter the correct input please.\n Type 'y' to guess or 'n' to continue ");
write("> ");
readf(" %s", &data);
guessY = data[0];
//fflush(&guessY);
}
writeln(guessY, " ",data);
}
And currently if I try and read the input in as a character array, I have to stop recieving input with ctrl+d and then it infinitely loops, if I try and recieve just a single character, if more than one character is typed, the next time you ask for input it will be automatically used. I've taken this small amount of code out of a larger function because this is where I isolated the issue to.
Change scanf(" %s", &data);
to scanf(" %s\n", &data);
.
Alternatively, you could just read a single character with: scanf(" %c", &guessY);