hello i'm trying to make this into a switch statment but i'm having trouble. Any help would be great!!! this is for a shell commands in C.

if(strcmp(command->comm[0], "exit") ==0)  //exit
    {
        printf("EXITING.....\n");
        break;
    }
    else if(strcmp(command->comm[0],"history")==0)   //history
     {

          int i=1;
          for(;i<hisSize;i++)
             printf(" %s ",history[i]);

     } //history end
     else if(strcmp(command->comm[0],"echo")==0)   //echo
     {
        int i=1;
        for(;i<command->args;++i)
            printf("%s",command->comm[i]);

         } //echo end   
     else if(strcmp(command->comm[0],"cd")==0)   //Change directory
         { 

        if(command->args > 2 )
        printf("Too many arguments for CD.\n");     

        else
       {
        if(command->args == 1)
        {
             cd = getenv("HOME");
             if(chdir(cd))
            printf("ERROR");   
        }
        else
        {
              cd = command->comm[1];
              if(chdir(cd))
            printf("ERROR");
        }
       }
     }//cd end

Recommended Answers

All 3 Replies

A switch works on integral types. The best you could do (to keep some sembelance of the command name) is to use an enum type - but you would still need to map strings to that.

You can't make it into a switch statement. switch only works on integers, never stings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.