Thread: How do I make a char switch case?

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    How do I make a char switch case?

    I'm making "Basic OS", but it really starts from DOS, XD oh well anyway...

    I want it to get your input after the ">>> ", right now I want to start off starting the 'exit' command, how do I make a switch that checks a char varaable, if theres a better way to do this plz post a reply.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TURE 1
    #define FALSE 0
    
    int _END=0;
    
    int main(int argc, char *argv[])
    {
      char name_login[5000];
      char command[500];
      
      printf("\n\n\tWelcome To BOS the Basic Oparateing System\n\n\nPlz Type in your name: ");
      scanf("%s", name_login);
      
      printf("\n\nHello %s", name_login);
      
      if(_END!=1)
      {
        printf("\n>>> ");
        scanf("&s", command);
        switch command
        {
            case1(command="exit") = {return 0;}
        
        }
         
      }else{}
      scanf("%s", command);
    }
    THX
    THERE IS NO PLACE LIKE 127.0.0.1

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You can't. switch requires an int.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by XSquared
    You can't. switch requires an int.
    Actually, a single char would work in a switch statement. However, strings will not. They could get close:
    Code:
    switch( tolower( command[0] ) )
    {
        case 'e':
            if( strcmp( command, "exit" ) == 0 )
            {
            }
            else
            if( strcmp( command, "extra" ) == 0 )
            {
            }
        break;
        case 'l':
            if( strcmp( command, "lalala" ) == 0 )
            {
            }
        break;
        /* and so on */
    }
    But again, to clarify, it requires an integral data type. This means no floating point numbers, no strings, no structures. Only things that can be considered 'int' in type.

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

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use strcmp when comparing strings, not = (or as it should be, ==).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are quite a few threads on here that show basic code for a shell. Try searching around a bit and see what you find.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Unhappy

    heres the code now:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TURE 1
    #define FALSE 0
    
    int _END=0;
    
    int main(int argc, char *argv[])
    {
      char name_login[5000];
      char command[500];
      
      printf("\n\n\tWelcome To BOS the Basic Oparateing System\n\n\nPlz Type in your name: ");
      scanf("%s", name_login);
      
      printf("\n\nHello %s", name_login);
      
      if(_END!=1)
      {
        printf("\n>>> ");
        scanf("&s", command);
        switch( tolower( command[0] ) ){
          case 'e': 
          {
              if( strcmp( command, "exit" ) == 0 )
              {
                  printf("exiting");
              }
              
              _END=1;
              break;
          }
          
          default: {printf("unknown command");break;}
        }
       }
    
      return 0;
    }
    but now the output is this:
    Code:
            Welcome To BOS the Basic Oparateing System
    
    
    Plz Type in your name: Karl
    
    
    Hello Karl
    >>> unknown command
    wat do I need to fix and how?
    THERE IS NO PLACE LIKE 127.0.0.1

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Also, look up flushing the input buffer.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>_END
    Variable names starting with an underscore followed by an uppercase letter are reserved, so you shouldn't be using them. I suggest to change it to END, loosing the underscore.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    what about taking advantage of argv by reading in the user name from the command prompt?

    Is this meant to run more than once? if so, use a loop and not an if
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM