Entering and splitting string
Hello. I have a problem trying to split and store specific string.
When user enters: Name1 10, Name 2 15, Name 3 40 I have to split it and store Names and age for other actions - like name sorting. So I have the problem of how to split and store the information correctly.
Code:
char *buf=(char*)malloc(MAX);
fflush(stdin);
char data[MAX];
char name_age[MAX];
system("cls");
printf("Enter name and age:\n");
gets(buf);
strcpy(data, buf);
fflush(stdin);
char delims[] = ", ";
char *result = NULL;
result = strtok(data, delims);
while(result != NULL) {
printf("Entered information: %s\n", result);
result = strtok(NULL, delims);
}
Can you help me with this. I found the strtok() function and separated the string to:
Name1 20
Name2 30
and so on
but I can't figure out how to store the information and use it afterwards.
I'll appreciate any help.