I can store everything just fine. But I run into problems when implementing an array within a linked list. The baratok is supposed to hold number values until the the input given is -1. But when I try to print it, it only gives me -1. So what could be a proper implementation for this?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getl(char s[], int lim)
{
int c,i;
for(i = 0; i < lim && (c = getchar()) != '\n' && c!=EOF; i++)
s[i] = c;
s[i] = '\0';
while(c != '\n' && c!=EOF)
c=getchar();
return i;
}
struct szemelylista {
int szemelyId;
char nev[50];
int *baratok;
struct szemelylista *kov;
} ;
int main()
{
struct szemelylista *llist = NULL, *elozo, *horgony;
int ct = 0, barat, i;
char s[50];
horgony = elozo = NULL;
while(printf("Name: ") && getl(s, 50))
{
if(!(llist = (struct szemelylista*)malloc(sizeof(struct szemelylista)))) {
break;
}
if(horgony) {
elozo ->kov = llist;
} else {
horgony = llist;
}
elozo = llist;
llist -> kov = NULL;
llist->szemelyId = ct;
strcpy(llist->nev, s);
printf("Friends: "); //this is the problematic part I guess
getl(s, 50);
barat = atoi(s);
llist->baratok = barat;
while(barat != -1) {
getl(s, 50);
barat = atoi(s);
llist->baratok = barat; //until here
}
ct++;
}
llist = horgony;
printf("ID\Name\Friends\tFriends with text\n");
printf("--------------------------------------------\n");
while(llist) {
printf("\t%d\t%s\t%d\t\n", llist->szemelyId, llist->nev, llist->baratok);
llist = llist->kov;
}
}