digitalmars.D.learn - Problem with using readln.
- JV (29/29) Apr 29 2017 Hello i'm kinda new to D language and i wanted to make a simple
- Adam D. Ruppe (3/7) Apr 29 2017 That shouldn't even compile... are you sure that's your actual
- JV (4/11) Apr 29 2017 umm im using code blocks so far it compiled and runs i don't know
- Adam D. Ruppe (3/4) Apr 29 2017 That's not necessary, it doesn't change anything.
- JV (4/8) Apr 29 2017 okay?? but how do i return an int?
- JV (2/7) Apr 29 2017 my bad for wrong import
- Andrew Edwards (25/35) Apr 29 2017 Basically the simplest way would be:
- arturg (4/7) Apr 29 2017 is there a reason you mix normal call and ufc or just some style?
- JV (3/32) Apr 29 2017 I added the () at readln() got deleted when editing the code
- Ivan Kazmenko (22/26) Apr 30 2017 One way would be:
Hello i'm kinda new to D language and i wanted to make a simple program but somehow my input does no go to my if statements and just continues to ask for the user to input.Kindly help me btw here is my sample code int func; writeln("\t\tEnter Selection : "); func = readln; writeln(func); if(func == 1) { writeln("hello world"); } if(func == 2) { writeln("hello world"); } if(func == 3) { writeln("endtime"); } if(func == 4) { writeln("hello world"); } if(func == 5) { writeln("hello world"); }
Apr 29 2017
On Sunday, 30 April 2017 at 02:07:48 UTC, JV wrote:int func; writeln("\t\tEnter Selection : "); func = readln; writeln(func);That shouldn't even compile... are you sure that's your actual code, and that it is actually building successfully?
Apr 29 2017
On Sunday, 30 April 2017 at 03:04:49 UTC, Adam D. Ruppe wrote:On Sunday, 30 April 2017 at 02:07:48 UTC, JV wrote:umm im using code blocks so far it compiled and runs i don't know why btw i forgot to add () at readln while editing the postint func; writeln("\t\tEnter Selection : "); func = readln; writeln(func);That shouldn't even compile... are you sure that's your actual code, and that it is actually building successfully?
Apr 29 2017
On Sunday, 30 April 2017 at 03:10:25 UTC, JV wrote:btw i forgot to add () at readln while editing the postThat's not necessary, it doesn't change anything. But readln without arguments returns a string, not an int.
Apr 29 2017
On Sunday, 30 April 2017 at 03:18:04 UTC, Adam D. Ruppe wrote:On Sunday, 30 April 2017 at 03:10:25 UTC, JV wrote:okay?? but how do i return an int? tried using what i found in the internet like using stdio.conv; to use toInt() but still shows errorbtw i forgot to add () at readln while editing the postThat's not necessary, it doesn't change anything. But readln without arguments returns a string, not an int.
Apr 29 2017
On Sunday, 30 April 2017 at 03:20:20 UTC, JV wrote:On Sunday, 30 April 2017 at 03:18:04 UTC, Adam D. Ruppe wrote:On Sunday, 30 April 2017 at 03:10:25 UTC, JV wrote:okay?? but how do i return an int? tried using what i found in the internet like using std.conv; to use toInt() but still shows errormy bad for wrong import
Apr 29 2017
On Sunday, 30 April 2017 at 03:20:20 UTC, JV wrote:On Sunday, 30 April 2017 at 03:18:04 UTC, Adam D. Ruppe wrote:Basically the simplest way would be: int val; readf("%d", &val); But if you are dead set on using readln, you will need to parse the line to obtain the integer value. The following demonstrates: // Scherkl-Nielsen self-important lookup template Module(string moduleName) { mixin("import Module = " ~ moduleName ~ ";"); } void main() { with(Module!"std.stdio: readln, writeln") with(Module!"std.conv: parse") { string line; parse!int((line = readln)).writeln; } } Your attempt failed because there is a '\n' character sitting at the end of the line read in by readln. It cannot be converted to an integer so any attempt to do so will result in an Exception being thrown. As was the case with toInt. -- AndrewOn Sunday, 30 April 2017 at 03:10:25 UTC, JV wrote:okay?? but how do i return an int? tried using what i found in the internet like using stdio.conv; to use toInt() but still shows errorbtw i forgot to add () at readln while editing the postThat's not necessary, it doesn't change anything. But readln without arguments returns a string, not an int.
Apr 29 2017
On Sunday, 30 April 2017 at 05:53:09 UTC, Andrew Edwards wrote:string line; parse!int((line = readln)).writeln;is there a reason you mix normal call and ufc or just some style? you can do this and remove some () (line = readln).parse!int.writeln;
Apr 29 2017
On Sunday, 30 April 2017 at 02:07:48 UTC, JV wrote:Hello i'm kinda new to D language and i wanted to make a simple program but somehow my input does no go to my if statements and just continues to ask for the user to input.Kindly help me btw here is my sample code int func; writeln("\t\tEnter Selection : "); func = readln(); writeln(func); if(func == 1) { writeln("hello world"); } if(func == 2) { writeln("hello world"); } if(func == 3) { writeln("endtime"); } if(func == 4) { writeln("hello world"); } if(func == 5) { writeln("hello world"); }I added the () at readln() got deleted when editing the code before i posted. sorry
Apr 29 2017
On Sunday, 30 April 2017 at 03:13:14 UTC, JV wrote:On Sunday, 30 April 2017 at 02:07:48 UTC, JV wrote:On Sunday, 30 April 2017 at 03:13:14 UTC, JV wrote: int func; writeln("\t\tEnter Selection : "); func = readln; readln(func); if(func == 1) { writeln("hello world"); } if(func == 2) { writeln("hello world"); } if(func == 3) { writeln("endtime"); } if(func == 4) { writeln("hello world"); } if(func == 5) { writeln("hello world"); } else { display(); } Mybad i posted the wrong oneHello i'm kinda new to D language and i wanted to make a simple program but somehow my input does no go to my if statements and just continues to ask for the user to input.Kindly help me btw here is my sample code int func; writeln("\t\tEnter Selection : "); func = readln(); writeln(func); if(func == 1) { writeln("hello world"); } if(func == 2) { writeln("hello world"); } if(func == 3) { writeln("endtime"); } if(func == 4) { writeln("hello world"); } if(func == 5) { writeln("hello world"); }I added the () at readln() got deleted when editing the code before i posted. sorry
Apr 29 2017
On Sunday, 30 April 2017 at 03:18:39 UTC, JV wrote:On Sunday, 30 April 2017 at 03:13:14 UTC, JV wrote:On Sunday, 30 April 2017 at 02:07:48 UTC, JV wrote:int func; writeln("\t\tEnter Selection : "); readln(func);
Apr 29 2017
On Sunday, 30 April 2017 at 02:07:48 UTC, JV wrote:Hello i'm kinda new to D language and i wanted to make a simple program but somehow my input does no go to my if statements and just continues to ask for the user to input.Kindly help meOne way would be: import std.stdio; int x; readf (" %s", &x); The "%s" means "default format for the type", which is I believe "%d" (decimal) for the int type. The space before "%s" is to skip all whitespace before the actual input, it will matter when you read your second integer: readf ("%s%s", &x, &y); // error, got space for y instead of a digit readf ("%s %s", &x, &y); // ok Another way is: import std.conv, std.stdio, std.string; int x = readln.strip.to!int; Here, we read the line with readln, strip the trailing whitespace with strip, and convert the resulting string to an int with to!int. Also, you might want to look at the corresponding chapter in Ali Cehreli's book: http://ddili.org/ders/d.en/input.html Ivan Kazmenko.
Apr 30 2017