Switch Statement with Char

Discussion in 'Mac Programming' started by SkippyThorson, Feb 19, 2010.

  1. SkippyThorson macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #1
    I tried to stay away as long as possible, but to no avail. :rolleyes:

    This time, it's a problem where a user enters a letter (b,g,o,y) and is given a return statement depending on the letter (char) entered.

    Code:
    #include <stdio.h>
    int main(void)
    
    {
    	//variables
    	char answer = 'x';
    	
    	//input
    	printf ("Welcome.\n");
    	printf("Enter the first letter of the cylinder's color: \n");
    	scanf("%d", &answer);
    	
    	//output based on the users input
    	//outcomes depend on the letter entered
    	switch(answer)
    	{
    		case 'b': case 'B': printf("The content is carbon monoxide.\n");
    			break;
    		case 'g': case 'G': printf("The content is oxygen.\n");
    			break;
    		case 'o': case 'O': printf("The content is ammonia.\n");
    			break;
    		case 'y': case 'Y': printf("The content is hydrogen.\n");
    			break;
    		default: printf("The content(s) of the cylinder is unknown.\n");
    			break;
    	}
    	
    	//end
    	printf("Thank you. \n");
    	return 0;
    }
    
    The user is prompted for a letter, and the program takes in to consideration a lower or upper case. If the letter entered does not match any case, an unknown is returned - a match returns it's corresponding statement.

    My problem today is that no matter what, it returns the default - unless I change the 'x' to one of the 4 letters. Any hints are appreciated. :) Thank you.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Why are you using %d as your scanf format? %d is for an integer as a signed decimal number. You should use the correct format for a character (consult the documentation).
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Are b,g,o, and y numbers? If not, is %d the right format specifier for scanf?

    -Lee

    EDIT: Bah! Beaten
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    One minute makes all the difference. Everyone seems to troll around trying to one up each other solving all the problems :p
     
  5. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #5
    One way to debug and help see what is going on in situations like this is to add some code to print out the values, particularly in the "unknown" case.

    So, instead of simply printing "The content of the cylinder is unknown", you should print out your variable "answer" as well. So it might print "No match for Q. The content of the cylinder is unknown".
     
  6. SkippyThorson thread starter macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #6
    Really!? Since the class began, I don't believe she's mentioned anything other than %d. I'll read up. :) Hard to believe she missed it, I must have.

    -- and that's a really good idea I'll keep in mind. However, our instructions include using just what is in the code, so I'm gonna play it safe on this one.

    Thanks again folks! :)
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Sometimes it's easier to lurk, waiting for the wrong answers...

    You don't have to hand in every speck of code you write. You can add something to help you understand or debug it, then take it out in order to meet the specification, er, I mean assignment.
     
  8. SkippyThorson thread starter macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #8
    I do that - I turn in the program, and jot down everything else in documentation at the bottom after. If I didn't, it would be put in a box in the back of my mind, and then lost in a closet. Or something close.
     
  9. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #9
    That's good. Don't be afraid to play around, try different techniques, or print extra things out so you get an idea of what's going on. When it's time to hand it in, stick to what your assignment dictates and use the methods suggested by the teacher, but on your own time, you'll learn a lot by playing around just for fun (and conversely, not being afraid to rip things apart when you are debugging).

    At work I have become largely responsible for a program that contains about half a million lines of code. Hundreds of classes, dozens of processes running concurrently across 3 CPU boards. Even with the tools I have available sometimes I still need to get in there and print "the value of X is ___" so I can keep track of what's going on. Obviously those statements get removed once I find the problem and fix it!
     
  10. SkippyThorson thread starter macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #10
    I had to laugh when I read "not being afraid to rip things apart" - that's my big thrill with the things that worked. I love taking something that's just fine, absolutely demolishing it, and then rebuilding it.

    Regarding the 500,000 lines... ~kneel/bow~

    Regarding the format specifiers, I found this handy site that covered some basics, and includes a table with them. My gears; they spin.
     
  11. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #11
    Do you have a programming book? It should have a reference section at the back.
     
  12. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #12
    The problem with using "%d" and a char is that "%d" tells scanf that the buffer provided is 32 bits, but a char is only 8 bits. Therefore, you have a potentially stack-smashing buffer overflow. Furthermore, it tells scanf to expect an integer, which letters are not, so it moves on and doesn't change the input buffer (which is probably part of why your program doesn't crash and is affected most by the initial value).
     

Share This Page