Thread: Switch inside While issues

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Switch inside While issues

    I'm trying to make a very basic program that is supposed to mimic a tv of sorts, its supposed to be for learning switch statements, and i am supposed to do it within a while loop. Here is what i have.

    Code:
    #include <stdio.h>
    
    int chan = 5, i = 4;
    char rem;
    
    int main (int argc, char * const argv[]) {
        while (i){
    		printf("\nCurrent channel: %d", chan);
    		printf("\nUse your remote (e,p,s,m,c):");
    		scanf("%c",&rem);
    		switch (rem){
    			case 'e':
    				return 0;
    				break;
    			case 'm':
    				printf("Muted");
    				getchar();
    				break;
    			case 'p':
    				printf("Playing");
    				getchar();
    				break;
    			case 's':
    				printf("Stopping");
    				getchar();
    				break;
    			case 'c':
    				printf("\nEnter Desired Channel: ");
    				scanf("%d",&chan);
    				break;
    				
    		}
    	}
    	
    		
    			
        return 0;
    }
    My problem seems to be that it skips the scanf within the loop everytime i write to the variable chan. Any ideas why? I realize i need a default but that didn't seem to fix anything, and will be added.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is a normal problem with scanf leaving a newline in the console buffer when grabbing ints.. It will be that way every time, so the best way to deal with it is add a getchar() after the scanf to soak up the newline:
    Code:
    	case 'c':
    				printf("\nEnter Desired Channel: ");
    				scanf("%d",&chan);
    				getchar();
    				break;
    I guess it is a "hack", but it works, and AFAIK there is no other solution (other than just ignoring the "problem").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because "enter-key" is a perfectly good character, but not part of a perfectly good integer. So when you hit "8 enter-key" at the prompt, the 8 is eaten but the enter-key is left behind for the next scanf.

    Probably the easiest fix is to tell the second read to ignore spaces/enter-keys by using a space:
    Code:
    scanf(" %c", &rem);

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Thanks alot guys ill try those 2 things. Thats alot!

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Probably the easiest fix is to tell the second read to ignore spaces/enter-keys by using a space:
    Okay, so AFAIK there is this other slightly nicer, more optimized, fix (thanks tabstop) but it still looks like a hack.
    Last edited by MK27; 02-16-2009 at 01:50 PM. Reason: optimization
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Okay, so AFAIK there is this other slightly nicer fix (thanks tabstop) but it still looks like a hack.
    That may be true, but the advantage is that every single getchar() call in the program goes away, AFAICT.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    The space in the scanf worked great. Thanks for the quick reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaring variables inside loops
    By samus250 in forum C Programming
    Replies: 21
    Last Post: 04-30-2008, 01:51 PM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM