Thread: help with switch case

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    45

    help with switch case

    Hi, Im fairly new to programming with C and was wondering if anyone could tell me whats wrong with this code:
    Code:
    #include <stdio.h>
    
    main() {
    	char input;
    	char h[200];
    	char b[200];
    	
    	printf("enter input: ");
    	scanf("s", input);
    	
    	switch (input) {
    		case "h":
    			printf("hi!\n");
    			break;
    		case "b":
    			printf("bye\n");
    			break;
    		default:
    			printf("incorrect input\n");
    			break;
    	}
    }
    and output:
    Code:
    switch.c:12: error: case label does not reduce to an integer constant
    switch.c:15: error: case label does not reduce to an integer constant
    Thanks for reading.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because "h" is not an integer constant. 'h' is an integer constant.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    45

    Smile

    Thanks for that.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    i compiled and ran it and when i type h or b it prints incorrect input all the time, any idea why this is??

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cprog12 View Post
    i compiled and ran it and when i type h or b it prints incorrect input all the time, any idea why this is??
    Try scanf( "%c" , &input);

    Your formatting string is probably what's causing the trouble.

    Also you have declared h and b arrays but are not using them.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    ok thanks Tater.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    it wont compile:
    Code:
    :~$ gcc '/home/bren/Program Files/switch.c'
    /home/bren/Program Files/switch.c: In function ‘main’:
    /home/bren/Program Files/switch.c:9: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘int’

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    If it helps, heres the new code:
    Code:
    #include <stdio.h>
    
    main() {
    	char input;
    	char h;
    	char b;
    	
    	printf("enter input: ");
    	scanf("%c", input);
    	
    	switch (input) {
    		case 'h':
    			printf("hi!\n");
    			break;
    		case 'b':
    			printf("bye\n");
    			break;
    		default:
    			printf("incorrect input\n");
    			break;
    	}
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cprog12 View Post
    If it helps, heres the new code:
    Code:
    #include <stdio.h>
    
    main() {
    	char input;
    	char h;
    	char b;
    	
    	printf("enter input: ");
    	scanf("%c", input);
    	
    	switch (input) {
    		case 'h':
    			printf("hi!\n");
    			break;
    		case 'b':
    			printf("bye\n");
    			break;
    		default:
    			printf("incorrect input\n");
    			break;
    	}
    }
    Yep, my bad... don't need the & for the input variable.

    You still don't need to define variables char b; and char h; ... your program, as shown actually makes no use of them.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    kk, it works now thanks for your help.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Code:
    	char input;
    	char h;
    	char b;
    	
    	printf("enter input: ");
    	scanf("%c", &input);

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Yep, my bad... don't need the & for the input variable.
    You do if you are using %c and a single char:
    Code:
    char c;
    scanf( "%c", &c );
    The reason you don't need it in the first example is because he's reading into an array, and scanning for %s; which, by using the name of the array, gives you a pointer to the first element. All of scanf's arguments are to be pointers (addresses).

    Edit: Think of what the function actually does: It takes arguments, and puts values in them. The only way you can do that, is if you are passing the address of the variable you want to fill.

    Quzah.
    Last edited by quzah; 12-25-2010 at 07:32 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    You do if you are using %c and a single char:
    Code:
    char c;
    scanf( "%c", &c );
    Would you believe too many eggnogs?

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    Would you believe too many eggnogs?
    Frankly I can hardly believe anyone drinks that stuff...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Frankly I can hardly believe anyone drinks that stuff...
    Well, I put up with it for the rum hidden within...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM