www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - readf error bug?

reply Dave Akers <dave dazoe.net> writes:
The following...

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);

	writefln("Got it: there are %d students.", studentCount);
	writefln("And there are %d teachers.", teacherCount);
}


When given the input...
10
42

will produce the error...
std.conv.ConvException /usr/include/dlang/dmd/std/conv.d(2013): 
Unexpected '4' when converting from type LockingTextReader to type int

I understand what is wrong and how to fix it but the produced error is 
incorrect.
May 09 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 9 May 2015 at 08:30:31 UTC, Dave Akers wrote:
 The following...

 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);

 	writefln("Got it: there are %d students.", studentCount);
 	writefln("And there are %d teachers.", teacherCount);
 }


 When given the input...
 10
 42

 will produce the error...
 std.conv.ConvException /usr/include/dlang/dmd/std/conv.d(2013):
 Unexpected '4' when converting from type LockingTextReader to 
 type int

 I understand what is wrong and how to fix it but the produced 
 error is
 incorrect.
To '\ n' does not remain in the input stream, it is necessary to write so: ----- readf("%s ", &studentCount);
May 09 2015
parent reply "Dave Akers" <dave dazoe.net> writes:
On Saturday, 9 May 2015 at 08:34:42 UTC, Dennis Ritchie wrote:
 On Saturday, 9 May 2015 at 08:30:31 UTC, Dave Akers wrote:
 The following...

 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);

 	writefln("Got it: there are %d students.", studentCount);
 	writefln("And there are %d teachers.", teacherCount);
 }


 When given the input...
 10
 42

 will produce the error...
 std.conv.ConvException /usr/include/dlang/dmd/std/conv.d(2013):
 Unexpected '4' when converting from type LockingTextReader to 
 type int

 I understand what is wrong and how to fix it but the produced 
 error is
 incorrect.
To '\ n' does not remain in the input stream, it is necessary to write so: ----- readf("%s ", &studentCount);
I was meaning this as a bug report. But wanted to point out your solution does not work. I was interested in D back in 1.0 days but soo much has changed in d 2.0 that I'm reading through the "Programming in D" book to learn all the new stuff and came across this odd un-helpful error. ... readf(" %s", &studentCount); ... readf(" %s", &teacherCount); ... that way it will ignore any non int chars left on the stdin stream.
May 09 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Saturday, 9 May 2015 at 08:41:49 UTC, Dave Akers wrote:
 On Saturday, 9 May 2015 at 08:34:42 UTC, Dennis Ritchie wrote:
 On Saturday, 9 May 2015 at 08:30:31 UTC, Dave Akers wrote:
 The following...

 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);

 	writefln("Got it: there are %d students.", studentCount);
 	writefln("And there are %d teachers.", teacherCount);
 }


 When given the input...
 10
 42

 will produce the error...
 std.conv.ConvException /usr/include/dlang/dmd/std/conv.d(2013):
 Unexpected '4' when converting from type LockingTextReader to 
 type int

 I understand what is wrong and how to fix it but the produced 
 error is
 incorrect.
To '\ n' does not remain in the input stream, it is necessary to write so: ----- readf("%s ", &studentCount);
I was meaning this as a bug report.
issues.dlang.org please, otherwise no-one will remember to fix it.
May 09 2015
parent Dave Akers <dave dazoe.net> writes:
On Sat, 09 May 2015 09:23:23 +0000, John Colvin wrote:

 On Saturday, 9 May 2015 at 08:41:49 UTC, Dave Akers wrote:
 On Saturday, 9 May 2015 at 08:34:42 UTC, Dennis Ritchie wrote:
 On Saturday, 9 May 2015 at 08:30:31 UTC, Dave Akers wrote:
 The following...

 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);

 	writefln("Got it: there are %d students.", studentCount);
 	writefln("And there are %d teachers.", teacherCount);
 }


 When given the input...
 10 42

 will produce the error...
 std.conv.ConvException /usr/include/dlang/dmd/std/conv.d(2013):
 Unexpected '4' when converting from type LockingTextReader to type
 int

 I understand what is wrong and how to fix it but the produced error
 is incorrect.
To '\ n' does not remain in the input stream, it is necessary to write so: ----- readf("%s ", &studentCount);
I was meaning this as a bug report.
issues.dlang.org please, otherwise no-one will remember to fix it.
Submitted... or rather found a similar one and updated it. https://issues.dlang.org/show_bug.cgi?id=12260
May 09 2015