www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Input error with readf()

reply "Paul" <paul example.com> writes:
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? 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
...
Jan 30 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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 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
Jan 30 2015
parent "Paul" <paul example.com> writes:
On Friday, 30 January 2015 at 15:03:55 UTC, Ali Çehreli wrote:

 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. :)
Maybe just remove the section with the 'incorrect' syntax? The reader will have to take it as read that " %s" is the correct form :D
Jan 30 2015