Jump to page: 1 26  
Page
Thread overview
Why I could not cast string to int?
Feb 02, 2012
xancorreu
Feb 02, 2012
David Nadlinger
Feb 02, 2012
xancorreu
Feb 02, 2012
Adam D. Ruppe
Feb 02, 2012
xancorreu
Feb 02, 2012
bearophile
Feb 02, 2012
bearophile
Feb 02, 2012
Ali Çehreli
Feb 02, 2012
Jonathan M Davis
Feb 02, 2012
Jonathan M Davis
Feb 02, 2012
xancorreu
Feb 02, 2012
Ali Çehreli
Feb 02, 2012
Jonathan M Davis
Feb 03, 2012
xancorreu
Feb 03, 2012
xancorreu
Segment violation (was Re: Why I could not cast string to int?)
Feb 02, 2012
xancorreu
Feb 02, 2012
bearophile
Feb 02, 2012
xancorreu
Feb 02, 2012
Timon Gehr
Feb 02, 2012
H. S. Teoh
Feb 02, 2012
Timon Gehr
Feb 02, 2012
Ali Çehreli
Feb 02, 2012
H. S. Teoh
Feb 02, 2012
H. S. Teoh
Feb 02, 2012
H. S. Teoh
Feb 02, 2012
bearophile
Feb 02, 2012
Jonathan M Davis
Feb 03, 2012
bearophile
Feb 03, 2012
Artur Skawina
Feb 03, 2012
Artur Skawina
Feb 03, 2012
bearophile
Feb 03, 2012
Artur Skawina
Feb 04, 2012
Timon Gehr
Feb 04, 2012
bearophile
Feb 04, 2012
Daniel Murphy
Feb 04, 2012
Artur Skawina
Feb 04, 2012
Timon Gehr
Feb 04, 2012
Artur Skawina
Feb 04, 2012
Timon Gehr
Feb 04, 2012
Artur Skawina
Feb 03, 2012
xancorreu
Feb 02, 2012
Timon Gehr
Feb 02, 2012
xancorreu
Feb 02, 2012
Tobias Pankrath
Re: Segment violation (was Re: Why I could not cast string to int?)
Feb 03, 2012
Jonathan M Davis
Feb 04, 2012
Timon Gehr
Feb 03, 2012
Artur Skawina
Feb 03, 2012
Jonathan M Davis
February 02, 2012
Hi,

In this code, how can I cast the args[0] string to int for computing factorial(args[0])?

import std.stdio, std.bigint, std.string, std.conv, std.stream;

BigInt recFactorial(int n) {
if (n == 0)
return BigInt(1);
else
return (BigInt(n) * recFactorial(n - 1));
}

void main(string[] args) {
if (args.length == 2)
writeln("Factorial requires a number");
else
try {
writeln(recFactorial(cast(int) args[1]));
} catch {
writeln("Error");
}
}



I receive:
$ gdmd-4.6 factorial.d
factorial.d: In function ‘main’:
factorial.d:15:0: error: cannot cast expression of type string to int


Thanks in advance,
Xan.
February 02, 2012
On 2/2/12 1:35 PM, xancorreu wrote:
> In this code, how can I cast the args[0] string to int for computing
> factorial(args[0])?

std.conv.to!int(…) or parse().

Hope this helps,
David
February 02, 2012
Use to!int(args[1])
> I receive:
> $ gdmd-4.6 factorial.d
> factorial.d: In function ‘main’:
> factorial.d:15:0: error: cannot cast expression of type string to int
> 
> 
> Thanks in advance,
> Xan.

February 02, 2012
Al 02/02/12 16:58, En/na David Nadlinger ha escrit:
> On 2/2/12 1:35 PM, xancorreu wrote:
>> In this code, how can I cast the args[0] string to int for computing
>> factorial(args[0])?
>
> std.conv.to!int(…) or parse().

to!int gives me this error:

$ ./factorial 222
Factorial requires a number



parse() where is the doc?

Thanks in advance,
Xan.
>
> Hope this helps,
> David

February 02, 2012
On Thursday, 2 February 2012 at 16:21:39 UTC, xancorreu wrote:
> $ ./factorial 222
> Factorial requires a number

args[0] is the name of your program.
(The first thing you typed on the command
line.)

Use args[1] to get that number.

> parse() where is the doc?

http://www.d-programming-language.org/phobos/std_conv.html#parse


to!int(args[1]) is probably what you want here though.
February 02, 2012
Al 02/02/12 16:58, En/na David Nadlinger ha escrit:
> On 2/2/12 1:35 PM, xancorreu wrote:
>> In this code, how can I cast the args[0] string to int for computing
>> factorial(args[0])?
>
> std.conv.to!int(…) or parse().

Sorry, if condition was wrong. conv.to!int is perfect!

Thanks,
>
> Hope this helps,
> David

February 02, 2012
Why cast(int) does not work and I have to call another function?

Thanks,

Al 02/02/12 17:24, En/na Adam D. Ruppe ha escrit:
> On Thursday, 2 February 2012 at 16:21:39 UTC, xancorreu wrote:
>> $ ./factorial 222
>> Factorial requires a number
>
> args[0] is the name of your program.
> (The first thing you typed on the command
> line.)
>
> Use args[1] to get that number.
>
>> parse() where is the doc?
>
> http://www.d-programming-language.org/phobos/std_conv.html#parse
>
>
> to!int(args[1]) is probably what you want here though.

February 02, 2012
I get "segment violation" error with  ./factorial 400000
How can I resolve it?

My code is:

import std.stdio, std.bigint, std.string, std.conv, std.stream;

BigInt recFactorial(int n) {
    if (n == 0)
        return BigInt(1);
    else
        return (BigInt(n) * recFactorial(n - 1));
}

void main(string[] args) {
    if (args.length != 2)
        writeln("Factorial requires a number");
    else
        try {
            writeln(recFactorial(std.conv.to!int(args[1])));
        } catch {
            writeln("Error");
        }
}



Thanks a lot,
February 02, 2012
On 02-02-2012 17:26, xancorreu wrote:
> Why cast(int) does not work and I have to call another function?
>
> Thanks,
>
> Al 02/02/12 17:24, En/na Adam D. Ruppe ha escrit:
>> On Thursday, 2 February 2012 at 16:21:39 UTC, xancorreu wrote:
>>> $ ./factorial 222
>>> Factorial requires a number
>>
>> args[0] is the name of your program.
>> (The first thing you typed on the command
>> line.)
>>
>> Use args[1] to get that number.
>>
>>> parse() where is the doc?
>>
>> http://www.d-programming-language.org/phobos/std_conv.html#parse
>>
>>
>> to!int(args[1]) is probably what you want here though.
>

Because D is a strongly typed language. Casting a string to an int doesn't make sense from a type system perspective.

--
- Alex
February 02, 2012
Alex Rønne Petersen Wrote:

> On 02-02-2012 17:26, xancorreu wrote:
> > Why cast(int) does not work and I have to call another function?
> >
> > Thanks,
> >
> > Al 02/02/12 17:24, En/na Adam D. Ruppe ha escrit:
> >> On Thursday, 2 February 2012 at 16:21:39 UTC, xancorreu wrote:
> >>> $ ./factorial 222
> >>> Factorial requires a number
> >>
> >> args[0] is the name of your program.
> >> (The first thing you typed on the command
> >> line.)
> >>
> >> Use args[1] to get that number.
> >>
> >>> parse() where is the doc?
> >>
> >> http://www.d-programming-language.org/phobos/std_conv.html#parse
> >>
> >>
> >> to!int(args[1]) is probably what you want here though.
> >
> 
> Because D is a strongly typed language. Casting a string to an int doesn't make sense from a type system perspective.
> 
> --
> - Alex

« First   ‹ Prev
1 2 3 4 5 6