digitalmars.D - 2 param Problems
- =?ISO-8859-1?Q?Fabian_Cla=dfen?= <admin fabs-world.de> Nov 30 2008
- Walter Bright <newshound1 digitalmars.com> Nov 30 2008
- =?ISO-8859-1?Q?Fabian_Cla=dfen?= <admin fabs-world.de> Nov 30 2008
- Derek Parnell <derek psych.ward> Nov 30 2008
Hi
I've learned the D language for a few days.
I've worked in other languages before.
But now I have following problems:
First:
import std.stdio;
double add(double param[] ...) {
double result = 0;
foreach(double d; param) {
result += d;
}
return result;
}
int main() {
double zahl;
zahl = add(2, 5.6, 7.8);
writefln("%d", zahl);
return 0;
}
___________________________________________________________
Second:
import std.stdio;
import std.format;
int main() {
double zahl = add(1, 5.6, 41, "Hello");
writefln("%d", zahl);
return 0;
}
double add(...) {
double result = 0;
for(int i=0; i < _arguments.length; i++) {
if(_arguments[i] == typeid(double)) {
double var = *cast(double*)_argptr;
result += var;
_argptr += double.sizeof;
} else if(_arguments[i] == typeid(float)) {
float var = *cast(float*)_argptr;
result += var;
_argptr += double.sizeof;
} else if(_arguments[i] == typeid(int)) {
int var = *cast(int*)_argptr;
result += var;
_argptr += int.sizeof;
} else if(_arguments[i] == typeid(bool)) {
_argptr += bool.sizeof;
} else if(_arguments[i] == typeid(char[])) {
char[] var = *cast(char[]*)_argptr;
_argptr += var.sizeof;
}
}
return result;
}
When I try to run this codes there is printed following message in the cmd:
Error: std.format floating
Please help me. I have no idea.
Greetings
Fabian Claßen
Nov 30 2008
Fabian Claßen wrote:double zahl = add(1, 5.6, 41, "Hello"); writefln("%d", zahl);
When I try to run this codes there is printed following message in the cmd: Error: std.format floating Please help me. I have no idea.
%d is for integral arguments, not floating point ones. Try %s.
Nov 30 2008
Walter Bright Wrote:Fabian Claßen wrote:double zahl = add(1, 5.6, 41, "Hello"); writefln("%d", zahl);
When I try to run this codes there is printed following message in the cmd: Error: std.format floating Please help me. I have no idea.
%d is for integral arguments, not floating point ones. Try %s.
Thank you. Now does the code works. :D Amazing *lol I love the D programming language
Nov 30 2008
On Sun, 30 Nov 2008 10:58:50 -0800, Walter Bright wrote:Fabian Claßen wrote:double zahl = add(1, 5.6, 41, "Hello"); writefln("%d", zahl);
When I try to run this codes there is printed following message in the cmd: Error: std.format floating Please help me. I have no idea.
%d is for integral arguments, not floating point ones. Try %s.
Also note that you would use '%f' or '%g' if you wanted to force runtime type checking on your arguments. The '%s' does not force typechecking as it converts whatever argument it is given to a string. You would also use "%f" if you needed finer formatting details, such as "%5.2f" etc ... -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Nov 30 2008
Derek Parnell Wrote:On Sun, 30 Nov 2008 10:58:50 -0800, Walter Bright wrote:Fabian Claßen wrote:double zahl = add(1, 5.6, 41, "Hello"); writefln("%d", zahl);
When I try to run this codes there is printed following message in the cmd: Error: std.format floating Please help me. I have no idea.
%d is for integral arguments, not floating point ones. Try %s.
Also note that you would use '%f' or '%g' if you wanted to force runtime type checking on your arguments. The '%s' does not force typechecking as it converts whatever argument it is given to a string. You would also use "%f" if you needed finer formatting details, such as "%5.2f" etc ... -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Hi thanks for that information. You are a big help. ;) Greetings Fabian Claßen
Dec 01 2008









=?ISO-8859-1?Q?Fabian_Cla=dfen?= <admin fabs-world.de> 