-2

I was trying to make a shell interpreter similar to DOS in C language (for fun obviously)

And when i type clear, as shown in the code below it should make it so it clears the screen. But it doesn't.

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    char command[128];
    int loop = 0;
    void main(){
        clrscr();
        printf("Starting shell\n");
        clrscr();
        while ( loop == 0){
            printf("command:");
            scanf("%s", &command);
            if(command=='clear'){
                printf("Clearing screen");
                clrscr();
            }  

/** Other Code **/
nik123
  • 113
  • 1
  • 2
  • 11
  • 1
    `scanf("%s", command); if(!strcmp(command, "clear")){ printf("Clearing screen"); clrscr(); }` – BLUEPIXY Aug 20 '14 at 11:14
  • 1
    Lots of basic C errors here. Read the documentation on these functions and do some C tutorials. Use `scanf("%s", command)` and `if(!strcmp(command, "clear"))`. Strings in C use double quotes, not single. – lurker Aug 20 '14 at 11:14
  • _Don't_ use `scanf("%s", command)` but `fgets(command, sizeof commmand, stdin)`. – mafso Aug 20 '14 at 11:42

1 Answers1

1
if(command=='clear')

it's not valid string comparison. use strcmp to compare string in C.

It should be

if (!strcmp(command, "clear"))
{
   printf("Clearing screen");
   clrscr();
}
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73