simple console input's not working...

Mike Parker aldacron at gmail.com
Fri Jun 6 03:47:14 PDT 2008


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;
     }
}
==================================================



More information about the Digitalmars-d-learn mailing list