Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 06, 2008 simple console input's not working... | ||||
---|---|---|---|---|
| ||||
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? |
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to SodiumFree | 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. |
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to predaeus | 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 !
|
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to SodiumFree | 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;
}
|
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to SodiumFree | 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
|
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to SodiumFree | 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; } } ================================================== |
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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;
}
|
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to predaeus | Ah, thanks for the link to the docs too, was wondering where they were located. |
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tower Ty | Thanks, both examples make the explanation a lot clearer. Rather strange that something like this has made it into the book though. |
June 06, 2008 Re: simple console input's not working... | ||||
---|---|---|---|---|
| ||||
Posted in reply to SodiumFree | "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. |
Copyright © 1999-2021 by the D Language Foundation