www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with using readln.

reply JV <jrpv100997 yahoo.com> writes:
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
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent reply JV <jrpv100997 yahoo.com> writes:
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:
     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?
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 post
Apr 29 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 30 April 2017 at 03:10:25 UTC, JV wrote:
 btw i forgot to add () at readln while editing the post
That's not necessary, it doesn't change anything. But readln without arguments returns a string, not an int.
Apr 29 2017
parent reply JV <jrpv100997 yahoo.com> writes:
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:
 btw i forgot to add () at readln while editing the post
That's not necessary, it doesn't change anything. But readln without arguments returns a string, not an int.
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 error
Apr 29 2017
next sibling parent JV <jrpv100997 yahoo.com> writes:
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 error
my bad for wrong import
Apr 29 2017
prev sibling parent reply Andrew Edwards <edwards.ac gmail.com> writes:
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:
 btw i forgot to add () at readln while editing the post
That's not necessary, it doesn't change anything. But readln without arguments returns a string, not an int.
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 error
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. -- Andrew
Apr 29 2017
parent arturg <var.spool.mail700 gmail.com> writes:
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
prev sibling next sibling parent reply JV <jrpv100997 yahoo.com> writes:
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
parent reply JV <jrpv100997 yahoo.com> writes:
On Sunday, 30 April 2017 at 03:13:14 UTC, JV wrote:
 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
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 one
Apr 29 2017
parent JV <jrpv100997 yahoo.com> writes:
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
prev sibling parent Ivan Kazmenko <gassa mail.ru> writes:
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
One 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