why does my code stop responding...?

S

SS

It seems that my code locks my machine up after the " fclose(datafile);"
command
I guess I'm not following the next bit of code on properly.
Can someone help me out??

SS
============================================================================
==========
# include <stdio.h>
main ()
{
FILE *datafile;
datafile = fopen ("a:\\lab1-1-1.csv","w");
float Dt = 0.01;
float u = 1;
float Tmax = 2;
float t = 0 , e = 0, y1 = 0, y = 0;
float J = 0.02;
float k = 10;
float B = 0.5;
while (t<=Tmax)
{
e = u - y ;
y1 = y1 +(k* Dt*e);
y = y+Dt/J*(k*y1-B*y);
printf("%7.3f %7.3f \n",t,y);
fprintf(datafile,"%7.3f, %7.3f \n ", t,y);
t = t+Dt;
}
fclose (datafile);
//ok to here?

FILE *datafileb;
datafileb = fopen ("a:\\lab1-12.csv","w");
float Dt2 = 0.01;
float u2 = 1;
float Tmax2 = 2;
float t2 = 0 , e2 = 0, y12 = 0, y2 = 0;
float J2 = 0.02;
float k2 = 1;
float B2 = 0.5;
while (t2<=Tmax2);
{
e2 = u2 - y2 ;
y12 = y12 +(k2* Dt2*e2);
y2 = y2+Dt2/J2*(k2*y12-B2*y2);
printf("%7.3f %7.3f \n",t2,y2);
fprintf(datafileb,"%7.3f, %7.3f \n ", t2,y2);
t2 = t2+Dt2;
}
fclose (datafileb);
return 0;
}
 
R

Russell Hanneken

SS said:
It seems that my code locks my machine up after the " fclose(datafile);"
command
I guess I'm not following the next bit of code on properly.
Can someone help me out??
[ . . . ]
while (t2<=Tmax2);
[ . . . ]

Remove the semicolon. By putting the semicolon there, you're saying the
while loop's body is empty (i.e., the block you have immediately after is
treated as unrelated to the while loop). Since there's nothing in the body
of the while loop to make the while condition false, the loop continues
forever.
 
J

Jack Klein

It seems that my code locks my machine up after the " fclose(datafile);"
command
I guess I'm not following the next bit of code on properly.
Can someone help me out??

SS
============================================================================
==========

Aside from Russell's answer, your code is not legal in any version of
standard C.
# include <stdio.h>
main ()

If you are using a compiler that conforms to the latest C99 standard,
it is illegal to define a function without a return type. This would
have to be:

int main()

....or:

int main(void)

....and is better written this way under any version of C.
{
FILE *datafile;
datafile = fopen ("a:\\lab1-1-1.csv","w");
float Dt = 0.01;
float u = 1;
float Tmax = 2;
float t = 0 , e = 0, y1 = 0, y = 0;
float J = 0.02;
float k = 10;
float B = 0.5;
while (t<=Tmax)
{
e = u - y ;
y1 = y1 +(k* Dt*e);
y = y+Dt/J*(k*y1-B*y);
printf("%7.3f %7.3f \n",t,y);
fprintf(datafile,"%7.3f, %7.3f \n ", t,y);
t = t+Dt;
}
fclose (datafile);
//ok to here?

And if you are not using a C99 compiler, but an older one, it is
illegal to define the variable below, because all variable definitions
in a block must come before any executable code in the block.
FILE *datafileb;
datafileb = fopen ("a:\\lab1-12.csv","w");
float Dt2 = 0.01;
float u2 = 1;
float Tmax2 = 2;
float t2 = 0 , e2 = 0, y12 = 0, y2 = 0;
float J2 = 0.02;
float k2 = 1;
float B2 = 0.5;
while (t2<=Tmax2);
{
e2 = u2 - y2 ;
y12 = y12 +(k2* Dt2*e2);
y2 = y2+Dt2/J2*(k2*y12-B2*y2);
printf("%7.3f %7.3f \n",t2,y2);
fprintf(datafileb,"%7.3f, %7.3f \n ", t2,y2);
t2 = t2+Dt2;
}
fclose (datafileb);
return 0;
}

I would suggest, whatever compiler you are using, you find the options
to tell it to compile in standard C mode.
 
E

E. Robert Tisdale

SS said:
It seems that my code locks my machine up
after the " fclose(datafile);" command
I guess I'm not following the next bit of code on properly.
Can someone help me out?

> cat why.c
# include <stdio.h>

int main (int argc, char* argv[]) {
FILE* datafile = fopen("lab1-1-1.csv", "w");
float Dt = 0.01;
float u = 1;
float Tmax = 2;
float t = 0 , e = 0, y1 = 0, y = 0;
float J = 0.02;
float k = 10;
float B = 0.5;
while (t <= Tmax) {
e = u - y ;
y1 = y1 + k*Dt*e;
y = y + Dt/J*(k*y1 - B*y);
fprintf(stdout, "%7.3f %7.3f\n", t, y);
fprintf(datafile, "%7.3f, %7.3f\n ", t, y);
t = t + Dt;
}
fclose(datafile);
//ok to here?

FILE* datafileb = fopen("lab1-12.csv", "w");
float Dt2 = 0.01;
float u2 = 1;
float Tmax2 = 2;
float t2 = 0 , e2 = 0, y12 = 0, y2 = 0;
float J2 = 0.02;
float k2 = 1;
float B2 = 0.5;
while (t2 <= Tmax2) {
e2 = u2 - y2;
y12 = y12 +(k2* Dt2*e2);
y2 = y2 + Dt2/J2*(k2*y12 - B2*y2);
fprintf(stdout, "%7.3f %7.3f \n", t2, y2);
fprintf(datafileb, "%7.3f, %7.3f \n ", t2, y2);
t2 = t2 + Dt2;
}
fclose(datafileb);
return 0;
}
> gcc -Wall -std=c99 -pedantic -O2 -o why why.c
> ./why
0.000 0.500
0.010 1.125
.
.
.
2.000 0.986

It works just fine for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top