www.digitalmars.com         C & C++   DMDScript  

D - stream.d bug [scanf() & getc()]

reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
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: +
Jul 12 2003
parent reply Helmut Leitner <helmut.leitner chello.at> writes:
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
Jul 12 2003
parent reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
"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
Jul 12 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
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

 then press enter, both scanf() and getc() ignores the next two calls.

 on the third call will the user get a chance to input additional
 information.

 Andrew

Jul 12 2003
parent reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
"Sean L. Palmer" <palmer.sean verizon.net> wrote...
 It would probably be enlightening for you if you print out the ascii code

 each character received instead of the characters themselves.

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
Jul 12 2003
parent reply "Vathix" <vathix dprogramming.com> writes:
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


 of
 each character received instead of the characters themselves.

The question then, should be: How do I flush the buffer after extracting

 first character? The closest thing I can find is fflush(), which does not
 work on stdin.

 Thanks,
 Andrew

Jul 12 2003
parent "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
"Vathix" <vathix dprogramming.com> wrote...
 I think, like in C, you have to take the newline out of the stream first

 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

 throw a ReadError.

IC... I'll try it out sometime tomorrow evening after I finish my homework! Thanks, Andrew
Jul 12 2003