1

I'm trying to insert a data from text file into a linked list, the data contains

ID-name
12345678-abcdefgh

But i don't know why the ID always print strange numbers, and i can't seem to find any errors in my code. Do anyone have any idea what makes the ID prints random number such as:

-1069759088
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>

struct mhs{

    int nim;
    char name[1000];

    struct mhs *next;

};

int main(){

    struct mhs *head, *node, *curr;
    struct mhs *hashT[1000];
    int nim;
    char name[100];
    int i, size = 0;
    int choice;
    head = curr = NULL;

    node = (struct mhs *)malloc(sizeof(struct mhs));

    FILE *fp;
    fp = fopen("datamhs.txt", "r");
    if(fp != NULL){
        while(fscanf(fp, "%d-%[^\n]", &nim, name) != EOF){
            fflush(stdin);
            strcpy(node->name, name);
            node -> nim = nim;
            printf("%d\n", node->nim);
            printf("%s\n", node->name);

            system("pause");
            node -> next = NULL;
            if(head == NULL){
                head = node;
            } else {
                curr = head;
                while(curr -> next != NULL){
                    curr = curr -> next;
                }
                curr -> next = node;
            }
            size++;
        }
    } else {
        printf("Error, no file to read\n");
    }

By the way, the output of the program is

-1069759088
abcdefgh

0 Answers0