www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - simple console input's not working...

reply SodiumFree <bob pistachios.com> writes:
So i copy this code, pretty much straight from the book (page 139):

module inputTest;
import tango.io.Console;

void main(){
    Cout("What is your name? ").flush;
    auto name = Cin.readln;
    Cout("Hello ")(name).newline;
}


However when i try compiling, dmd spits out this error:

C:\d.stuff\inputTest>dmd inputTest.d
inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
inputTest.d(6): Error: expected 2 arguments, not 0
inputTest.d(7): function alias tango.io.Console.Console.Output.append (char[])
does not match parameter types (bool)
inputTest.d(7): Error: expected 0 arguments, not 1


Any idea what's going on?
Jun 05 2008
next sibling parent reply predaeus <predaeus gmx.net> writes:
SodiumFree Wrote:
 inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
 ...
 Any idea what's going on?

From: http://www.dsource.org/projects/tango/docs/current/tango.io.Console.html " ... # final bool readln (ref char[] content, bool raw = false); Retreive a line of text from the console and map it to the given argument. The input is sliced, not copied, so use .dup appropriately. Each line ending is removed unless parameter raw is set to true. Returns false when there is no more input. ... " So, the syntax of readln is a bit different. You have to pass it the string that will hold the data.
Jun 06 2008
next sibling parent Tower Ty <towerty msn.com.au> writes:
predaeus Wrote:

 SodiumFree Wrote:
 inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
 ...
 Any idea what's going on?

From: http://www.dsource.org/projects/tango/docs/current/tango.io.Console.html " ... # final bool readln (ref char[] content, bool raw = false); Retreive a line of text from the console and map it to the given argument. The input is sliced, not copied, so use .dup appropriately. Each line ending is removed unless parameter raw is set to true. Returns false when there is no more input. ... " So, the syntax of readln is a bit different. You have to pass it the string that will hold the data.

I don't blame you if you are confused , so am I . It should be pretty simple at this level and the book examples should at least work . They sometimes don't and I'm buggered if I can understand why. The guy that answers certainly does not put it clearly Lets hope some kind soul drops in and explains it to both of us !
Jun 06 2008
prev sibling parent SodiumFree <bob pistachios.com> writes:
Ah, thanks for the link to the docs too, was wondering where they were located.
Jun 06 2008
prev sibling next sibling parent Tower Ty <towerty msn.com.au> writes:
SodiumFree Wrote:

 So i copy this code, pretty much straight from the book (page 139):
 
 module inputTest;
 import tango.io.Console;
 
 void main(){
     Cout("What is your name? ").flush;
     auto name = Cin.readln;
     Cout("Hello ")(name).newline;
 }
 
 
 However when i try compiling, dmd spits out this error:
 
 C:\d.stuff\inputTest>dmd inputTest.d
 inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
 inputTest.d(6): Error: expected 2 arguments, not 0
 inputTest.d(7): function alias tango.io.Console.Console.Output.append (char[])
does not match parameter types (bool)
 inputTest.d(7): Error: expected 0 arguments, not 1
 
 
 Any idea what's going on?

While we wait this at least compiles but it is meaningless module test4; import tango.io.Console; void main(){ Cout("What is your name? ").flush; char[] message= "What is your pleasure?"; auto name = Cin.readln(message); Cout("Hello ").newline; }
Jun 06 2008
prev sibling next sibling parent Tower Ty <towerty msn.com.au> writes:
SodiumFree Wrote:

 So i copy this code, pretty much straight from the book (page 139):
 
 module inputTest;
 import tango.io.Console;
 
 void main(){
     Cout("What is your name? ").flush;
     auto name = Cin.readln;
     Cout("Hello ")(name).newline;
 }
 
 
 However when i try compiling, dmd spits out this error:
 
 C:\d.stuff\inputTest>dmd inputTest.d
 inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
 inputTest.d(6): Error: expected 2 arguments, not 0
 inputTest.d(7): function alias tango.io.Console.Console.Output.append (char[])
does not match parameter types (bool)
 inputTest.d(7): Error: expected 0 arguments, not 1
 
 
 Any idea what's going on?

Here is another one with an output module test4; import tango.io.Console; void main(){ Cout("What is your name? ").flush; char[] name; Cin.readln(name); name~= "Hello "; Cout(name).newline; } Strange
Jun 06 2008
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
SodiumFree wrote:
 So i copy this code, pretty much straight from the book (page 139):
 
 module inputTest;
 import tango.io.Console;
 
 void main(){
     Cout("What is your name? ").flush;
     auto name = Cin.readln;
     Cout("Hello ")(name).newline;
 }
 
 
 However when i try compiling, dmd spits out this error:
 
 C:\d.stuff\inputTest>dmd inputTest.d
 inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
 inputTest.d(6): Error: expected 2 arguments, not 0
 inputTest.d(7): function alias tango.io.Console.Console.Output.append (char[])
does not match parameter types (bool)
 inputTest.d(7): Error: expected 0 arguments, not 1
 
 
 Any idea what's going on?

Cin.readln (which is the method tango.io.Console.Console.Input.readln) expects at least one argument -- a string in which to store the input. You've given it nothing. The compiler reports this in the first error, then keeps on compiling. Second, 'name' is automatically inferred as a bool and not as a string as you seem to intend it to be. You'll see in the documentation that readln returns true if input is read and false if not. So the compiler sees you trying to pass a bool value in the call to Cout when it expects a string, resulting in the second error. A look at the source for tango.io.Console should make the error messages more clear. Console is a struct with the inner classes Input and Output. Cin is an instance of Console.Input. Hence the tango.io.Console.Console.Input.readln (char[],bool) in the error string. In Console.output, the append method is aliased to opCall. That's what allows the COut()() syntax. It also is the reason you see tango.io.Console.Console.Output.append (char[]). Your corrected code: ====================================================== import tango.io.Console; void main() { Cout("What is your name? ").flush; char[] name; if(Cin.readln(name)) { Cout("Hello ")(name).newline; } } ==================================================
Jun 06 2008
parent reply Tower Ty <towerty msn.com.au> writes:
Mike Parker Wrote:

 SodiumFree wrote:
 So i copy this code, pretty much straight from the book (page 139):
 
 module inputTest;
 import tango.io.Console;
 
 void main(){
     Cout("What is your name? ").flush;
     auto name = Cin.readln;
     Cout("Hello ")(name).newline;
 }
 
 
 However when i try compiling, dmd spits out this error:
 
 C:\d.stuff\inputTest>dmd inputTest.d
 inputTest.d(6): function tango.io.Console.Console.Input.readln (char[],bool)
does not match parameter types ()
 inputTest.d(6): Error: expected 2 arguments, not 0
 inputTest.d(7): function alias tango.io.Console.Console.Output.append (char[])
does not match parameter types (bool)
 inputTest.d(7): Error: expected 0 arguments, not 1
 
 
 Any idea what's going on?

Cin.readln (which is the method tango.io.Console.Console.Input.readln) expects at least one argument -- a string in which to store the input. You've given it nothing. The compiler reports this in the first error, then keeps on compiling. Second, 'name' is automatically inferred as a bool and not as a string as you seem to intend it to be. You'll see in the documentation that readln returns true if input is read and false if not. So the compiler sees you trying to pass a bool value in the call to Cout when it expects a string, resulting in the second error. A look at the source for tango.io.Console should make the error messages more clear. Console is a struct with the inner classes Input and Output. Cin is an instance of Console.Input. Hence the tango.io.Console.Console.Input.readln (char[],bool) in the error string. In Console.output, the append method is aliased to opCall. That's what allows the COut()() syntax. It also is the reason you see tango.io.Console.Console.Output.append (char[]). Your corrected code: ====================================================== import tango.io.Console; void main() { Cout("What is your name? ").flush; char[] name; if(Cin.readln(name)) { Cout("Hello ")(name).newline; } } ==================================================

Good Mike , and this goes too module test4; import tango.io.Console; void main(){ Cout("What is your name? ").flush; char[] name; Cin.readln(name); char[] lead = "Hello "; Cout(lead)(name).newline; }
Jun 06 2008
parent reply SodiumFree <bob pistachios.com> writes:
Thanks, both examples make the explanation a lot clearer. Rather strange that
something like this has made it into the book though.
Jun 06 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"SodiumFree" <bob pistachios.com> wrote in message 
news:g2c3e2$2v9h$1 digitalmars.com...
 Thanks, both examples make the explanation a lot clearer. Rather strange 
 that something like this has made it into the book though.

It's probably because Tango is not complete, and has been in a constant state of flux for the past year and a half since it was announced. When that chapter of the book was written, that probably was the interface to Cin.readln.
Jun 06 2008
next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
Jarrett Billingsley wrote:
 "SodiumFree" <bob pistachios.com> wrote in message 
 news:g2c3e2$2v9h$1 digitalmars.com...
 Thanks, both examples make the explanation a lot clearer. Rather strange 
 that something like this has made it into the book though.

It's probably because Tango is not complete, and has been in a constant state of flux for the past year and a half since it was announced. When that chapter of the book was written, that probably was the interface to Cin.readln.

I know it's in alpha, but I really wish the Tango devs (and Phobos, too) would put more thought into backwards compatibility. Even if it's just a deprecated function to forward to the new API, I'd rather it be there to allow old programs to compile. </complaining about something I didn't pay for or help with>
Jun 06 2008
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Jarrett Billingsley" wrote
 "SodiumFree" <bob pistachios.com> wrote in message 
 news:g2c3e2$2v9h$1 digitalmars.com...
 Thanks, both examples make the explanation a lot clearer. Rather strange 
 that something like this has made it into the book though.

It's probably because Tango is not complete, and has been in a constant state of flux for the past year and a half since it was announced. When that chapter of the book was written, that probably was the interface to Cin.readln.

I don't have a copy of the book, but I think it is just a book typo. According to this changeset: http://www.dsource.org/projects/tango/changeset/2094 readln always had the current interface since it was introduced. What the book should have done was used copyln (or get): void main(){ Cout("What is your name? ").flush; auto name = Cin.copyln; // or Cin.get Cout("Hello ")(name).newline; } -Steve
Jun 09 2008