Yes or No Options

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 27 09:05:47 PDT 2015


On Monday, 27 July 2015 at 15:50:11 UTC, Alex wrote:
> 	readf(" %s", &dieNumber);

What happens here is a bit tricky and trips up a lot of 
programmers: readf leaves the end-of-line character in the 
buffer, which readln then sees as meaning its job is done.

When you enter, say, 5, then press enter, the input looks like: 
"5\n". (\n represents the newline aka end-of-line character). 
readf " %s" first skips any spaces. There's none, so it doesn't 
matter.

Then it reads in a number. So it takes the 5, leaving the rest of 
the input behind.... so that \n from pressing enter is still 
there.


readln reads everything it can until it hits a \n, picking up 
where readf left off. So the result is it immediately sees the \n 
that readf stopped on and it also stops.

An easy fix is to stick an extra readln() after the readf:

         readf(" %s", &dieNumber);
         readln(); // tell it to skip the rest of the line
         // the rest of your code is unchanged




The next thing you'll have to face is actually going again when 
they enter Y. (BTW be aware that this is case sensitive and 
readln leaves the newline at the end of the input too! So 
entering "y" won't work. And when you enter "Y"... the program 
will see "Y\n", so it won't match "let's go again" either.)

But to actually go again, you'll probably want to read about 
loops. I'll let you play with it a little from here though.


More information about the Digitalmars-d-learn mailing list