-
program loops twice
Code:
for(;;){
printf("0) Exit\n");
printf("1) Run\n");
scanf("%c", &response);
switch (response) {
case '0':
return 0;
case '1':
break;
}
for(i=0,t=0.0;t<TOTTIME;t+=DT,i++) {
vs = A * sin(2*PI*F*t);
ir1 = (vs-v1-v2)/R1;
ir2 = (v1-v2)/R2;
ir3 = v2/R3;
v1 = v1 + DT * (ir1-ir2)/C1;
v2 = v2 + DT * (ir2-ir3)/C2;
if(i%DTPS) continue;
printf("%f\t%f\t%f\t%f\n",t,vs,v1,v2);
fprintf(f,"%f\t%f\t%f\t%f\n",t,vs,v1,v2);
}}
this code is supposed to take response, print the 2nd part of the code, do the first again and ask for another response. Instead, it does it twice, not asking for a response. it executes like this:
Code:
0) Exit
1) Run
1
//data prints here
0) Exit
1) Run
//data prints here again
0) Exit
1) Run
//awaits response
the code isnt complete, obviously, but I just need to know how fix this one error. thx
-
What happens when you input a letter? You enter press the letter key, and then what? You press enter, right? There's your second character, and thus, your second loop. There are entries in the FAQ section describing various methods of reading input from the user.
Quzah.
-
I'm looking at the unbuffered input section in the faq, and it looks like it's explained in C++. How would this go down in C?
-
I don't think unbuffered input is possible. I'm sure it's been discussed many times here.
-
It certainly isn't possible without knowing which OS and compiler is being used...
-
well, I'm on winXPpro and lcc-win32
and if I can't do unbuffered input, how else can I fix it?
-
Clear the input stream after you make your call (there's a FAQ on that too), or use one of the other methods for reading input in the FAQ.
Quzah.