Thread: switch-case statement not working properly

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43

    switch-case statement not working properly

    hello, i'm having a switch-case problem. if a letter is entered the default works but the program acts all crazy after that please help.

    Also for the switch statement to loop i left out the break after default is this ok?

    Code:
    #include <stdio.h>
    
    
    void std();
    void etc();
    
    
    int main(){
        int c;
    
        do {
              printf("Menu:\n\n");
              printf("\n1 - std");
              printf("\n2 - etc..");
              printf("\n0 - exit");
              printf("\n\nChoice: ");
              scanf("%d",&c);
    
              switch(c){
                            case 1: std();
                            break;
    
                            case 2: etc();
                            break;
    
                            case 0:
                            break;
    
                            default:
                                      printf("\n\nIncorrect Entry!,Please press enter and Try again!\n\n");// stops working if a letter is pressed
                                      getch();
               }
    
    
        }while(c!=0);
        
        
    }
    void std(){
         int std_id;
         char name[20];
         do{
            printf("enter student id or '0' to exit\n");
            scanf("%d",&std_id);
            while(std_id!=0){
            printf("enter students name\n");
            scanf("%s",&name);
            break;}
         }while(std_id!=0);
    }
    void etc(){
         
         
    }
    Last edited by Shinzu911; 04-25-2012 at 04:26 AM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Doesn't your compiler trigger warning: implicit declaration of function 'getch'?
    You need to include the appropriate header file.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    nope i have no warnings but which header file are u talking about just curious

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    besides the getch() there is an error in
    Code:
            scanf("%s",&name);
    guess that makes your "switch-case statement not working properly"
    Kurt

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are not compiling with all warnings then, make sure you have the -Wall flag on.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    And don't use getch() to begin with. Use getchar().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    what is the difference between getch() and getchar()

  8. #8
    Registered User
    Join Date
    Feb 2012
    Location
    Trinidad & Tobago
    Posts
    43
    @ zuk whats wrong with my scanf() function i don't see it

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You don't see it because you never bother to compile your code. You just immediately come here and post all the barf from your editor. COMPILE and post all warnings and errors here.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    "%s" expects a char *, you pass a char **
    Kurt

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    >> >> scanf("%s",name);

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if a letter is entered the default works but the program acts all crazy after that please help.
    Because the input stream cannot progress if you have "a" as the input, and your next scanf statement is say "%d".

    Read the FAQ on how to flush the input stream ( and no, it isn't fflush(stdin) )

    Better still, use fgets() to read ALL input, and then use sscanf() to parse the input. Check BOTH for success.

    This approach does not lock up / go crazy when presented with garbage input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement not working Properly
    By noname88 in forum C Programming
    Replies: 11
    Last Post: 09-29-2011, 08:44 PM
  2. can you place a if/else statement in a switch case
    By swansea in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2010, 08:59 PM
  3. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  4. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  5. How can I turn this code into a case switch statement?
    By Lonck in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2007, 09:14 AM