Page 1 of 1

why does this code got Compilation Error

Posted: Sat Jun 07, 2003 9:02 am
by lowai
it works well in VC++ 6.0
[cpp]
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#include <string.h>

double hypot(double x, double y)
{
return sqrt(x*x + y*y);
}


int main() {
int mins, p;
double x, y, xx, yy;
double dist = 0;
char line[80], s[80], temp[80], *ss;

int kase;
cin >> kase;
while (kase--) {
cin >> x >> y;
cin.getline(line, 80, '\n');
cin.getline(line, 80, '\n');

while (line[0] >= '0') {
p = strchr(line, ' ') - line + 1;
strncpy(s, line, p - 1);
x = atof(s);
ss = &line[p];
strcpy(temp, ss);
strcpy(line, temp);

p = strchr(line, ' ') - line + 1;
strncpy(s, line, p - 1);
y = atof(s);
ss = &line[p];
strcpy(temp, ss);
strcpy(line, temp);

p = strchr(line, ' ') - line + 1;
strncpy(s, line, p - 1);
xx = atof(s);
ss = &line[p];
strcpy(temp, ss);
strcpy(line, temp);

yy = atof(line);
dist += hypot( xx-x, yy-y );
cin.getline(line, 80, '\n');
}
dist /= 10000.0;
mins = (int)(dist * 60.0 + 0.5);
printf("%lf:%02lf\n", mins/60, mins%60);
if (kase) printf("\n");
}
return 0;
}

[/cpp][/c]

Posted: Sat Jun 07, 2003 8:12 pm
by rjhadley
$ g++ -ansi -Wall -o x x.cc
x.cc: In function `int main()':
x.cc:28: implicit declaration of function `int atof(...)'
x.cc:53: warning: use of `l' length character with `f' type character
x.cc:53: warning: use of `l' length character with `f' type character
FYI - atof() is in stdlib.h

Posted: Sun Jun 08, 2003 7:05 am
by lowai
but i still got 'compile error' after i added 'include <stdlib.h>'

Posted: Sun Jun 08, 2003 10:12 am
by Juergen Werner
I submitted your code above with "#include <stdlib.h>" added and it compiled fine. Maybe you accidently chose C instead of C++ as language...