Switch and case problem.

This is a discussion on Switch and case problem. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int fx(int a); int main(void) { int x; printf("Enter an integer: "); scanf("%d", &x); /* assume user ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Switch and case problem.

    Code:
    #include <stdio.h>
    
    	int fx(int a);
    
    	int main(void)
    {
    	int x;
    
    		printf("Enter an integer: ");
    		scanf("%d", &x); /* assume user enters 5 */
    
    	switch( x )
    	{
    		case 8: printf("%d\n", fx(4) );
    		case 6: printf("%d\n", fx(6) ); 
    		case 5: printf("%d\n", fx(8) ); 
    		case 3: printf("%d\n", fx(10) );
    		default: printf("%d\n", fx(12) );
    	}
    }
    	int fx(int a)
    {
    	return (a - 4);
    }
    Output
    Code:
    4
    6
    8
    Ok, I have no clue how the program gets the output. First I thought that for each of the cases, it gives a number to use as 'a'. But then I cant see how the number 5 activates 4, 6, and 8. Can someone explain it to me?

  2. #2
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    try putting break; after each case.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Well, the output is correct according to the example in the book, my problem is that I dont understand how the program came to that conclusion.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, assume x is 5 as the comment says and see where you end up from there.

    hint: switch(5) does what?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Look at the values passed into the fx function, there's your answer.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    So basically, in the printf cases, I plug that number in as 'a' down in the definition of the function? If so, then the first three 4, 6, and 8 values are all below 5...which I think would make sense why they appear in the output. To test this, I tried entering '1' as 'x' and the output gives me 12, and I assume that came from the 'default printf'. God I feel stupid.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    The problem is that the code continues to execute case after case until all cases from the initial case has been executed or it encounters a break. In your case, it goes through the cases 5, 3 and default if you enter 5.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 01:08 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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