dimanche 28 juin 2015

Function to read sentence from user input [C]

I am trying to read sentence from user input problem with my function is it skips second try when I try to call it... Any solution?

void readString(char *array, char * prompt, int size) {
    printf("%s", prompt);
    char c; int count=0;
    char * send = array;
    while ((c = getchar()) != '\n') {
        send[count] = c; count++;
        if (size < count){ free(array); break; } //lets u reserve the last index for '\0'
        }

    }

Here is how try to call it:

char obligation[1500];
char dodatno[1500];

readString(obligation, "Enter obligation", 1500);
readString(dodatno, "Enter hours", 1500);

Here is example of inputs: "This is some sentence"

so latter I wana do this:

printf(" %s | %s \n",obligation, dodatno);

and get:

This is some sentence|This is another sentence

Aucun commentaire:

Enregistrer un commentaire