Input error with readf()

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 30 07:03:55 PST 2015


On 01/30/2015 04:49 AM, Paul wrote:

 > If I run the following example program from the 'Programming in
 > D' book, the input doesn't 'get stuck' as stated in the book but
 > instead produces the error message given. Have things changed
 > since that part of the book was written or is it some other
 > issue?

Apparently, things did change since then. I will change the text 
accordingly. I would like to add you to the acknowledgments section if 
you email your full name to acehreli at yahoo.com. Thank you...

 > The version that uses " %s" in the call to readf works as
 > expected.
 >
 >
 > import std.stdio;
 >
 > void main(){
 >
 >      write("How many students are there? ");
 >      int studentCount;
 >      readf("%s", &studentCount);
 >      write("How many teachers are there? ");
 >      int teacherCount;
 >      readf("%s", &teacherCount);
 >
 >      writeln("Got it: There are ", studentCount, " students",
 >      " and ", teacherCount, " teachers.");
 > }
 >
 > How many students are there? 67
 > How many teachers are there? 5
 > std.conv.ConvException@/usr/include/dmd/phobos/std/conv.d(1968):
 > Unexpected '5' when converting from type LockingTextReader to
 > type int
 > ...

Not the best error message... Saying '5' is unexpected for 'int' is 
confusing, right? Unfortunately, I can't come up with a good explanation 
for that error message in the book. :)

Ali



More information about the Digitalmars-d-learn mailing list