0

Here's my code:

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

int main(void)
{
     char terminal[100];

     printf("Enter cmds: ");
     scanf(" %s", terminal);

     if(strcmp(terminal, "help") == 0){
           printf("test");
           scanf(" %s", terminal); // trying to detect if a user types
                                   // "help" the menu will pop up again
     }

     return 0;
     }

When a user types "help", the menu pops up, (good so far). But when they type "help" again, the menu does not pop up. Does anybody know what is going on?

3
  • 2
    where did you code for the again part? Commented Jan 29, 2017 at 14:56
  • Perhaps a do...while loop is what you want? Commented Jan 29, 2017 at 14:56
  • How is VB.NET related to this?? Commented Jan 29, 2017 at 14:57

1 Answer 1

2

The initial comments hit the nail on the head here. You need to loop over new input multiple times. This can be done fairly easily.

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

int main(void)
{
    char terminal[100];

    printf("Enter cmds: ");

    // this expression will return zero on invalid input, exiting the loop
    while (scanf("%99s", terminal) == 1) {  
        // your if statement and other code you want to repeat go here.
    }
}

To better encapsulate this kind of behaviour, defining some sort of function that compares strings and returns an element of an enum is a very common practice, but not required in this question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.