Strange problem with a c file with subroutines
Hi everyone,
I have a very weird problem with this program: with Visual C++ compiler it isn't compiled (the compiler says "missing ';' before 'type'" ) and with another IDE it works, but the return value is always 0.0. Could anyone help me? Thanks
#include
<stdio.h>#include
<stdlib.h>#include
<math.h>int
inCylinder_1(double,double,double);double
distance(double,double,double,double,double,double,double,double,double);int
main(){
double x,y,z;printf(
"%s","Insert point coordinates: ");//scanf("%f%f%f",&x,&y,&z);x =
1.0;y =
1.5;z =
0.5;int inCylinder = inCylinder_1(x,y,z);if(inCylinder ==0)printf(
"%s\n","false");elseprintf(
"%s\n","true");system(
"PAUSE");return0;}
double
distance(double a,double b,double c,double d,double e,double f,double x,double y,double z){
double dist1 = abs(a*x+b*y+c)/sqrt(a*a+b*b);double dist2 = abs(d*x+e*z+f)/sqrt(d*d+e*e);double result = sqrt(dist1*dist1 + dist2*dist2);return result;}
int
inCylinder_1(double x,double y,double z){
double a,b,c,d,e,f,radius;a =
0.;b =
1.;c = -
1.;d =
0.;e =
1.;f = -
1.;radius =
1.;double dist1 = abs(a*x+b*y+c)/sqrt(a*a+b*b);double dist2 = abs(d*x+e*z+f)/sqrt(d*d+e*e);double result = sqrt(dist1*dist1 + dist2*dist2);double dist = distance(a,b,c,d,e,f,x,y,z);printf(
"%f\n",result);if(result<radius)return1;elsereturn0;}
[6322 byte] By [
Cla82] at [2007-12-22]
What line is the compiler error pointing to?
I just tried compiling using VC8.0 and it compiles with no errors. Also, when the application is run, I get the following output:
Insert point coordinates: <some number>true
Press any key to continue . . .If you are still facing a problem, please try debugging your application and post if you have more details.
Thanks,Ayman ShoukryVC++ TeamVC++ 2005 finds an error at line 17 ("missing ';' before type"), line 18 ("'inCylinder' undeclared identifier), lines 48, 49, 50, 52, 54 (missing ';' before type). I think the compiler (or maybe the linker) can't see the subroutines... Thanks
Yes, in some IDEs the application is compiled, but the output is always 0, even if I change the point coordinates (x, y and z). If I have only one method ("main") it works perfectly, but when I split the code in those three methods it doesn't work anymore. Thank you anyway
I just tried once more using VC2005. It compiles with no errors.
The above code you posted exactly compiles and runs with above output I posted in my previous post.
What are the compiler options you are using? You can get that from the build log.
Thanks,Ayman ShoukryVC++ TeamI was able to reproduce the errors:
error C2143: syntax error : missing ';' before 'type'
error C2065: 'inCylinder' : undeclared identifier
error C2143: syntax error : missing ';' before 'type'
error C2143: syntax error : missing ';' before 'type'
error C2143: syntax error : missing ';' before 'type'
error C2143: syntax error : missing ';' before 'type'
error C2065: 'result' : undeclared identifier
You need to compile as .cpp and not .c. You can do that using the /TP compiler option. It is working in some IDEs since probably it is being compiled as C++ and not C
Thanks,Ayman ShoukryVC++ TeamThank you very much, but...why is it wrong in C syntax? It seems to be correct...
In C make sure you declare your variables first, for example the below sample will generate a similar error:
#include <stdio.h>
int main()
{
printf ("anything\n");
int x=6;
}
error C2143: syntax error : missing ';' before 'type'
To fix it, change the code to:
#include <stdio.h>
int main()
{
int x=6;
printf ("anything\n");
}
Hope this helps!
Thanks,Ayman ShoukryVC++ TeamActually the first piece of code doesn't give me errors... Anyway, even if I declare the variable first, the problem is still not fixed, I get 0.0000 for each call to
distance method
I found the problem: it was in
distance method, because of the use of math abs function (which only manages integers) instead of fabs (which manages doubles). Thank you all, guysI have that error for this reason. How can I set the compiler to accept variable declaration anywhere and still use the C compiler (/TC)?
Thanks,
Jean-Luc Wasmer
You can't - the C compiler that ships with Visual C++ does not allow this.