Thread overview | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 02, 2012 Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to xancorreu | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to xancorreu | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to xancorreu | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Segment violation (was Re: Why I could not cast string to int?) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to xancorreu | 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 Re: Why I could not cast string to int? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Rønne Petersen | 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
|
Copyright © 1999-2021 by the D Language Foundation