Thread: Switch Statements

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    Unhappy Switch Statements

    Is there any reason why the program would ignore a switch statement?

    I believe that its coded correctly, but none of the cases are acted upon. Thanks for any advice. Here's a copy of the switch code

    Code:
    switch (previousDives){
    	
    	
    	
    	case ' 0 ' :
    		penalty = 0;
    		printf (" The secondary dive penalty is %d minutes.\n" , penalty );
    		break;
    
    	case ' 1 ' :
    		penalty = 4;
    		printf (" The secondary dive penalty is %d minutes.\n" , penalty );
    		break;
    
    	case ' 2 ' :
    		penalty = 10;
    		printf (" The secondary dive penalty is %d minutes.\n" , penalty );
    		break;
    
    	case ' 3 ' :
    		penalty = 17;
    		printf (" The secondary dive penalty is %d minutes.\n" , penalty );
    		break;
    
    	case ' 4 ' :
    		penalty = 27;
    		printf (" The secondary dive penalty is %d minutes.\n" , penalty );
    		break;
    	}

    [edit]Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To start with, always use a default option in the case statement to trap errors like this.

    And the character consts should be like this: '1'
    That's without the spaces (unlike yours).


    Please use code tags when posting code. See my sig for a useful link on this matter.

    [edit]Dammit, beaten
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    I thought it was something to do with '1' being a character, and that you should remove the ' ' from it....

    so it'd be - case 1: /*something happens*/

    Or... why is it different in this instance?
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    thank you

    deleting the spaces fixed the problem! Thanks for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 02:59 PM
  3. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM