Darrenthebozz
Darrenthebozz

Reputation: 13

my C program crashing for an unseen reason

So my program is meant to be a little command line but it keeps crashing:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>    

void main()
{    
    char cmd;
    for(;;)
    {
        fgets(cmd,255,stdin);
        if (strstr(cmd,"CD")!=NULL )
        {
            cmd +=2;
            SetCurrentDirectory(cmd);
        }
        else
        {
            system(cmd);
        }
    }
}

the compiler output is lvalue expected.

Upvotes: 0

Views: 54

Answers (1)

liamcomp
liamcomp

Reputation: 386

Maybe you are looking to do something like this:

void main()
{    
    char cmd[255]; // this allocate a character array to store the command in
    for(;;)
    {
        fgets(cmd,255,stdin); // get the characters from stdin and store them in the cmd character array with a max length of 255
        if ( strncmp(cmd, "CD ", 3) == 0 ) // check if the first three characters are "CD "
        {
            SetCurrentDirectory(&cmd[3]); // pass the string not including the first 3 charcters, which should be "CD "
        }
        else
        {
            system(cmd);
        }
    }
}

Upvotes: 1

Related Questions