program loops twice

This is a discussion on program loops twice within the C Programming forums, part of the General Programming Boards category; 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++) ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    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?

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    I don't think unbuffered input is possible. I'm sure it's been discussed many times here.

  5. #5
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    It certainly isn't possible without knowing which OS and compiler is being used...

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    well, I'm on winXPpro and lcc-win32
    and if I can't do unbuffered input, how else can I fix it?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. simple program on loops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-11-2002, 11:24 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21