Thread overview
stream.d bug [scanf() & getc()]
Jul 12, 2003
Andrew Edwards
Jul 12, 2003
Helmut Leitner
Jul 12, 2003
Andrew Edwards
Jul 12, 2003
Sean L. Palmer
Jul 12, 2003
Andrew Edwards
Jul 12, 2003
Vathix
Jul 12, 2003
Andrew Edwards
July 12, 2003
Maybe the bug's just in my head; however, the following code produces undesired results!

Regards,
Andrew

------------code-----------
import stream;
void main()
{
  char something;

  // with scanf()
  stdout.printf("Type a character: ");
  stdin.scanf("%c",&something);
  stdout.printf("You typed: %c\n", something);
  stdout.printf("Now type something else: \n");
  stdin.scanf("%c",&something);
  stdout.printf("You typed: %c\n\n", something);

  // with getc()
  stdout.printf("Type a character: ");
  something = stdin.getc();
  stdout.printf("You typed: %c\n", something);
  stdout.printf("Now type something else: \n");
  something = stdin.getc();
  stdout.printf("You Typed: %c", something);
}

------------output-----------
C:\d>scan
Type a character: 2
You typed: 2
Now type something else:
You typed:

Type a character: You typed:

Now type something else:
+
You Typed: +


July 12, 2003

Andrew Edwards wrote:
> 
> Maybe the bug's just in my head; however, the following code produces undesired results!

I think it would be helpful if you would make clear what you desired.

--
Helmut Leitner    leitner@hls.via.at Graz, Austria   www.hls-software.com
July 12, 2003
"Helmut Leitner" <helmut.leitner@chello.at> wrote...
>
> I think it would be helpful if you would make clear what you desired.
>

Sorry!

I'm simply attempting to get two characters from the user with successive
calls to either scanf() or getc(). When first prompted, if the user inputs
multiple characters into the stream, the scanf() or getc() will read from
the remaining characters. However, if the user only inputs one character and
then press enter, both scanf() and getc() ignores the next two calls.  Only
on the third call will the user get a chance to input additional
information.

Andrew


July 12, 2003
It would probably be enlightening for you if you print out the ascii code of each character received instead of the characters themselves.

Sean

"Andrew Edwards" <edwardsac@spamfreeusa.com> wrote in message news:bep22g$18j8$1@digitaldaemon.com...
> "Helmut Leitner" <helmut.leitner@chello.at> wrote...
> >
> > I think it would be helpful if you would make clear what you desired.
> >
>
> Sorry!
>
> I'm simply attempting to get two characters from the user with successive
> calls to either scanf() or getc(). When first prompted, if the user inputs
> multiple characters into the stream, the scanf() or getc() will read from
> the remaining characters. However, if the user only inputs one character
and
> then press enter, both scanf() and getc() ignores the next two calls.
Only
> on the third call will the user get a chance to input additional information.
>
> Andrew


July 12, 2003
"Sean L. Palmer" <palmer.sean@verizon.net> wrote...
> It would probably be enlightening for you if you print out the ascii code
of
> each character received instead of the characters themselves.
>
IC...
The question then, should be: How do I flush the buffer after extracting the
first character? The closest thing I can find is fflush(), which does not
work on stdin.

Thanks,
Andrew


July 12, 2003
I think, like in C, you have to take the newline out of the stream first or it'll be the next input. So, perhaps you want to loop getc until it's \n, printing out only the first one. In C it could return EOF, but in D it could throw a ReadError.


"Andrew Edwards" <edwardsac@spamfreeusa.com> wrote in message news:bep99c$1fig$1@digitaldaemon.com...
> "Sean L. Palmer" <palmer.sean@verizon.net> wrote...
> > It would probably be enlightening for you if you print out the ascii
code
> of
> > each character received instead of the characters themselves.
> >
> IC...
> The question then, should be: How do I flush the buffer after extracting
the
> first character? The closest thing I can find is fflush(), which does not
> work on stdin.
>
> Thanks,
> Andrew
>
>


July 12, 2003
"Vathix" <vathix@dprogramming.com> wrote...
> I think, like in C, you have to take the newline out of the stream first
or
> it'll be the next input. So, perhaps you want to loop getc until it's \n, printing out only the first one. In C it could return EOF, but in D it
could
> throw a ReadError.


IC...
I'll try it out sometime tomorrow evening after I finish my homework!

Thanks,
Andrew