Sunday, October 30, 2022

[FIXED] How to make interactive cmd interface?

Issue

I was tasked with making an interactive cmd interface in C. I need to manage a data base of students, and have 3 commands which are : add,remove,print. I cant get the interactive platform right, I need achieve the following : each time I enter a command, it executes and then waits for new command without exiting, so the linked list won't get deleted of the memory, so far Im stuck here :

int main(int argc,char *argv[])
{
    char input[MAX];
    int c;
    if(argc == 1)
    {
        printf("no values were entered");
        exit(0);
    }

    while(--argc>0 )
    {
        ++argv;
        {
            strcpy(input,*argv);
            if((*++argv) )
            {
                printf("\nmain(): too much args\nexit(0)\n");
                exit(0);
            }
        }
    }

    {
        if (strcmp(input,"add") == 0)
            /*do something*/;
        else if(strcmp(input,"print") == 0)
            /*do something*/
        else if(strcmp(input,"remove") == 0)
            /*do something*/
        else
            printf("no command was found\n");
    }
    return 0;
}

Now, this does get my input and do what it is supposed to do, but I cant get my head around on how to make it return and wait for more commands without exiting the program. Thanks for all information and help in advance!


Solution

If you want make an interactive interface you should follow a few steps.

1 - Get the key press by the user like

keypress = getch();

2 - Switch case the key pressed like

switch(keypress)
{
    case 'a' :
       // Do somethink...
    case 'b' :
       // Do somethink
    default :
       break;
}

3 - Now put this in while loop like

do
{
    keypress = getch();
    switch(keypress)
    {
        case 'a' :
          // Do somethink...
        case 'b' :
          // Do somethink
        default :
          break;
}
while(keypress != 'q')
// quit program...

Thanks, Robin.



Answered By - Robin M.
Answer Checked By - Terry (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.