0

I'm creating two programs that form a game and will be communicating with each other where one of them is essentially a user, but I'm starting off with building one and typing myself to this first program.

I want to input commands such as 'turn d1d2d3d4d5d6' (where the di are dice rolls), 'rerolled d1d2d3d4d5d6' and so forth, theres a bunch of commands. I want my program called player to take these commands and they'll do something with it.

Just to begin with I'm trying to take the input using stdin and putting it in array, then checking the array to see if its a valid command. However I can't seem to use fgetc and the array correctly. What i'm doing currently is just to take the input, put into an array and print it.

I don't actually want it to be 128 size array, I want it to be completely adjustable but I don't know how to do this with fgets. The if loop to check if its NULL is to find out if an array is empty but that's defintely wrong, not sure what to put in place there.

while(1){
    int i = 1;
    char* command[128];
    fgets(command, 128, stdin);
    for (i=0; i < 128; i++){
        if (command[i] == NULL){
            printf("%c\n", command[i]);
        }
    }

    return 0;
}

So to be specific my main goal right now is take a command such as 'eliminated p' from the user and command becomes command=["eliminated","p"]

  • 2
    I think you meant to write `char command[128];`, in first place. – Sourav Ghosh Sep 19 '16 at 06:19
  • Secondly, it's too broad, as it reads now, let alone be unclear. Can you add some more details of _actually_ what you target to achieve? – Sourav Ghosh Sep 19 '16 at 06:20
  • 1
    Possible duplicate of [How can I read an input string of unknown length?](http://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length) – masoud Sep 19 '16 at 06:20
  • 3
    Look hard at POSIX function [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) — it gives you indefinitely long input lines. – Jonathan Leffler Sep 19 '16 at 06:22
  • Currently, I want to take a command which could be 'turn d1d2d3d4d5d6' or 'eliminated p' (p is a player but irrelevant) and then put that into an array called command. So for example for 'eliminated p', command is command=["eliminated", "p"] – Rav Chandra Sep 19 '16 at 06:23
  • You can try gets(command); – PaulHK Sep 19 '16 at 06:28
  • 3
    NO, NO, NO, NEVER try `gets` is has been removed from C11 due to security concerns. – David C. Rankin Sep 19 '16 at 06:36

1 Answers1

0

sample code:

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

int main(void) {
    while(1){
        char command[128];
        if(NULL == fgets(command, sizeof command, stdin))//or use getline
            break;
        if(strchr(command, '\n') == NULL){//Too long
            printf("Too long\n");
            while(getchar() != '\n')
                ;//clear input
            continue;
        }
        char *operation = strtok(command, " \t\n");
        if(operation == NULL){
            printf("Empty command\n");
            continue;
        }
        char *operand = strtok(NULL, " \t\n");;
        if(operand == NULL){
            printf("Empty operand\n");
            continue;
        }
        printf("operation:%s\noperand:%s\n", operation, operand);
        char *commands[] = {operation, operand};
        //do stuff
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70