Thread: Case and switch

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Case and switch

    Ok, I was trying out the switch and case thing and I have come close to it I think, my program works(in a way)but all I want it to do is take the user input and compare it to the values of the variables and print out the corresponding line, maybe the code will help out a bit:

    Code:
    #include <stdio.h>
    
    int a=1;
    int b=2;
    int c=3;
    int d;
    
    
    int main(void)
    {
    printf("Please enter either numbers 1,2 or 3 to see your selection: ");
    scanf("d");
    
    {
    	switch(d)
    	{
    	case 'a':
    		printf("This is line 'a'");
    
    	case 'b':
    		printf("This is line 'b'");
    
    	case 'c':
    		printf("This is line 'c'");
    	}
    
    return 0;
    }
    }
    I am not sure if that is even correct as far as the whole switch thing goes but I could use help on the use input thing if you don't mind.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    You're trying to take in an integer and check it in the "switch" against an ascii character. You should have been able to find this
    problem with a debug session.

    Try the following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int d;
    char input[BUFSIZ];
    
    int main(void)
    {
     printf("Please enter either numbers 1,2 or 3 to see your selection: ");
    
     // take in a character the safe way
     if(fgets(input,BUFSIZ,stdin)!=NULL)
     {
      // convert it to an integer
      sscanf(input, "%d", &d);
     }
    
     switch(d)
     {
      case 1: printf("This is line 'a'"); break;
    
      case 2:   printf("This is line 'b'"); break;
     
      case 3:  printf("This is line 'c'"); break;
    
      default :
       puts(" You didn't enter a 1 or 2 or 3"); break;
     } // end switch(d)
    
     return (0);
    } // end int main(void)
    Last edited by jerryvtts; 07-15-2002 at 12:12 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > scanf("d");

    Not to mention that nice piece of code...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok thats cool and I am going to try it but here is a question about the case thing itself. In my version I was trying to compare the value of 'd' to the cases, how does yours work? I can't see how or what you are checking...I am kinda confused about that if you don't mind explaining it...
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by CAP
    In my version I was trying to compare the value of 'd' to the cases, how does yours work? I can't see how or what you are checking...
    Well, in CAP's code this happened
    Code:
    scanf("d");  /* Wrong, as previously highlighted */
    
    switch(d)
    {
    case 'a':
    	printf("This is line 'a'");
    
    case 'b':
    	printf("This is line 'b'");
    
    case 'c':
    	printf("This is line 'c'");
    }
    And in jerryvtts's code, this happened:
    Code:
    if (fgets(input, BUFSIZ, stdin) != NULL)
    {
        sscanf(input, "%d", &d);
    }
    switch (d)
    {
    	case 1: printf("This is line 'a'"); break;
    	
    	case 2: printf("This is line 'b'"); break;
    	
    	case 3: printf("This is line 'c'"); break;
    	
    	default: puts(" You didn't enter a 1 or 2 or 3"); break;
    }
    The structure is basically the same, it just that jerry's will actually compile and run
    Also, note that jerry's case statements are checking against numbers, whereas CAP's are checking against character constants.

    CAP, quick question, why ask the user to enter 1, 2 or 3, then check their input again a, b and c? Sounds a bit fishy to me

    jerry, please can you use code tags for the code (see my sig for an example). It helps make the code readable by keeping the layout as it is in your editor. Thanks.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Like I said I was just trying out the case and switch thing, I wasn't interested in a good copy yet because I would be the only one using it(I am not in any classes or anything). I usually make good copies that make sense to other people later on after things actually work (if only they would though ) Sorry about the not proper syntax and way of handling things but I am trying.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

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, 05: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, 02:08 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM