Jump to page: 1 2
Thread overview
Problem with using readln.
Apr 30, 2017
JV
Apr 30, 2017
Adam D. Ruppe
Apr 30, 2017
JV
Apr 30, 2017
Adam D. Ruppe
Apr 30, 2017
JV
Apr 30, 2017
JV
Apr 30, 2017
Andrew Edwards
Apr 30, 2017
arturg
Apr 30, 2017
JV
Apr 30, 2017
JV
Apr 30, 2017
JV
Apr 30, 2017
Ivan Kazmenko
April 30, 2017
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");
    }
April 30, 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?

April 30, 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:
>>     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
April 30, 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
April 30, 2017
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.
April 30, 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:
>> 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
April 30, 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:
>> 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
April 30, 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 error

my bad for wrong import


April 30, 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);



April 30, 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:
>>> 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

« First   ‹ Prev
1 2