Why I could not cast string to int?

xancorreu xancorreu at gmail.com
Thu Feb 2 04:35:11 PST 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.


More information about the Digitalmars-d-learn mailing list