Thread: Please Help Me With Switch Statement Problem

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    3

    Please Help Me With Switch Statement Problem

    Hello! So I'm working on a little personal project. I have been coding in C for around a month and a half, but this is not my first language so I am not a complet beginner in programing.

    So onto my little problem. I made a small example of the problem.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    int keep_running = 1; //To keep loops running
    
    //Function Decl
    void list();
    void help();
    
    
    int main()
    {
      //Decl
      char input[81];   
        
     while( keep_running == 1) //Main Screen
     {
         printf("root@chips:~# ");     
         gets(input);
         
         switch(input)
         {
         case 'ls' : 
                     list();
                     break;
         case 'help' :
                     help();
                     break;
         default :
                 printf("Unknown Command");
                 break;
                 
         }
                 
         
                 
    
    
    } //while end
          
           
            
     } //Main end   
                  
    
    void list() 
    {    
         printf("FILES LISTED\n");
    }
    void help()
    {
         printf("HELP STUFF");
    }
    I'm using Dev-C++ 4.9.9.2 and I get error messages:

    C:\Users\*\Documents\C Projects\main.cpp In function `int main()':

    24 C:\Users\*\Documents\*\main.cpp switch quantity not an integer

    26:11 C:\Users\*\Documents\*\main.cpp [Warning] multi-character character constant

    29:11 C:\Users\*\Documents\*\main.cpp [Warning] multi-character character constant

    Not sure if I should of wrote strcmp (input, "ls") != 1 insted of just 'ls'

    Have I done something wrong? I tried using a if else statement but that did not go to well.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things to note before I get to your problem:
    • Avoid global variables: keep_running should be declared in the main function.
    • This declares a function named list that takes an unspecified number of arguments:
      Code:
      void list();
      Since you want the function to take no arguments, declare it with a single parameter of type void:
      Code:
      void list(void);
    • The gets function leaves you vulnerable to buffer overflow. One option is to use fgets instead, but keep in mind that fgets will store the newline character read if there is space.


    I'm using Dev-C++ 4.9.9.2 and I get error messages:

    C:\Users\*\Documents\C Projects\main.cpp In function `int main()':

    24 C:\Users\*\Documents\*\main.cpp switch quantity not an integer

    26:11 C:\Users\*\Documents\*\main.cpp [Warning] multi-character character constant

    29:11 C:\Users\*\Documents\*\main.cpp [Warning] multi-character character constant

    Not sure if I should of wrote strcmp (input, "ls") != 1 insted of just 'ls'

    Have I done something wrong? I tried using a if else statement but that did not go to well.
    Yes, you are doing something wrong. In C, a character constant is delimited by single quotes, e.g., 'a' is a character constant. A string constant is delimited by double quotes, e.g., "a" is a string constant. You used 'ls' and 'help', which are string constants, except that they have more than one character, hence the warnings about "multi-character character constant". I am not sure when it is appropriate to use these, but I am sure that it is wrong here.

    What you should do is to ditch the switch. Use an if-else chain, comparing strings. To compare strings, use strcmp from <string.h>, keeping in mind that strcmp returns 0 when the strings match.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    I switched to if else and it works, but I think I did it wrong because it gives weird outputs.

    Code:
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    
    //Function Decl
    void list();
    void help();
    
    
    int main()
    {
      //Decl
      char input[81];  
      int keep_running = 1; //To keep loops running 
        
     while( keep_running == 1) //Main Screen
     {
         printf("root@chips:~# ");     
         gets(input);
         
         if ( strcmp (input, "ls") != 0)
         {
              list();
         }
         if ( strcmp (input, "help") != 0)
         {
              help();
         }
         else
         {
              printf("Unknown Command\n");
         }
                 
         
                 
    
    
    } //while end
          
           
            
     } //Main end   
                  
    
    void list() 
    {    
         printf("FILES LISTED\n");
    }
    void help()
    {
         printf("HELP STUFF");
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're comparing wrongly. It should be strcmp (input, "ls") == 0. Furthermore, you need the else for the first if statement too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    That solved it. Thanks for the help!

  6. #6
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Avoid gets! Use fgets. And you compare string with "help" even if it is "ls" already. Rewrited in my style:

    Code:
    while( 1 ) {
       printf("root@chips:~# ");     
       if( !fgets(input, sizeof(input), stdin) ) { break; }
    
       if( strcmp(input, "ls\n") == 0 ) {           // fgets does not delete '\n'. Alternatively,
            list();                                // use function chomp (you can find many open-source
       } else if( strcmp(input, "help\n") == 0 ) {  // variations in Internet or just write your own).
            help();
       } else {
            printf("Unknown Command\n");
       }
    
    }
    
    void crop_string(char* s)
    {
        if( s = strpbrk(s, "\n\r") ) {
            *s = '\0';
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement in a TCP Server problem
    By csharp100 in forum C Programming
    Replies: 8
    Last Post: 04-28-2012, 09:51 AM
  2. Having a problem with a Switch Statement
    By ARod609 in forum C Programming
    Replies: 15
    Last Post: 07-13-2011, 06:57 PM
  3. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  4. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM