Beginner
Beginner

Reputation: 143

Function to read the data file as nodes in a linked list

My text file reads as this: George Washington, 2345678 John Adams, 3456789 Thomas Jefferson, 4567890 James Madison, 0987654 James Monroe, 9876543 John Quincy Adams, 8765432 Andrew Jackson, 7654321 Martin Van Buren, 6543210

However, when I run my current code to display I don't get those numbers and end up with random ones, how can I fix this? You can avoid the other functions for now as I am just trying to make sure the file reads into different nodes.

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

//Creates node for holding student's information
struct node
{
char name [50];
int id;
struct node *next;
}*head;

//Create Function Prototypes
void readDataFile ();
void display(struct node *d);
void deleteID(int num);
void deleteName(char dname);


//Main function
int main()
{
//Declare variables
int i, num, intid;
char *name;
char nameDelete [50];
char nameInsert [50];
struct node *n;

//initialize link list
head = NULL;

//Read in file
readDataFile();

//Create list of operations utilized in program
while (1)
{
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Delete by ID\n");
    printf("4.Delete by Name\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");

    if(scanf("%d", &i) <= 0)
    {
        printf("Enter only an Integer\n");
        exit(0);
    }
    else
    {
        switch(i)
        {
            case 1:
                printf("Enter the name to insert: ");
                gets(nameInsert);
                printf("Enter the ID associated with the name: ");
                scanf("%d", &intid);
                break;
            case 2:
                if (head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Elements in the list are:\n");
                }
                display(n);
                break;
            case 3:
                if(head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Enter the ID number to delete: ");
                    scanf("%d", &intid);
                }
                break;
            case 4:
                if(head == NULL)
                    printf("List is Empty\n");
                else
                {
                    printf("Enter name to delete: ");
                    gets(nameDelete);
                }
            case 5:
                return 0;
            default:
                printf("Invalid option\n");
        }
    }
}
return 0;
}

Upvotes: 1

Views: 1250

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409216

The problem you have is not the reading but the printing.

printf("Student %s has ID %d\n", d->name, &d->id);

You use the address-of operator & when reading with scanf (and family), but when you're printing it will print the address of the variable.


You also have some other problem with the reading of the file, besides what I mentioned in my comment, and that is your none-linking:

temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;

This will create an extra node at the end of the list, which will be uninitialized meaning that when you use the data it will lead to undefined behavior.

Actually, with the problem mentioned in my comment you will have two extra nodes.

Upvotes: 2

Related Questions