Hello everyone,
This is my first post, I'm having trouble on an assignment. I'm writing a function for a database program in class... I have to write the function that will tranverse a linked list, add a "node" and then write the user entered info into the "node".
The first printf is there just to prove to me that the next_id() function is working correctly, which it is, however when I get to the end the number that prints in the last printf statement is the first number in the linked list... This means I'm ovrwritting the first keyfile in the linked list...
This is the type definition that the proffesor gave us, and everything compiles, just doesn't function correct, I hope its something small and I appreciate any help!
Thanks,
Doug
/* This is the type definition file for the data base management
system code. Include it in source files that need it and contact
the project manager (Sean) if you want any definitions added to
the file. */
#define null (KEYS)0
#define chnull (char *)0
#define L 19
#define MAXLINES 200
#define CHARS 132
struct DATE {
int month,day,year;
};
typedef struct DATE Date;
typedef struct Key* KEYS;
struct Key {
char *sender;
char *addressee;
char *regarding;
Date date;
int id;
char *fname;
KEYS next;
};
typedef struct Key KEY;
#include <stdio.h>
#include "keytype.h"
void add(KEYS k)
{
int next_id(KEYS list);
int nextID;
KEY* curr;
curr = k; /* set curr to head node */
nextID = next_id(k);
printf("%d\n", nextID);
if( curr->next ) /* loop through linked list to find last node */
curr = curr->next; /* bump pointer to next node */
else {
curr->next = (KEY*)malloc(sizeof(KEY)); /* allocate space for next node */
curr = curr->next; /* bump pointer to next node */
curr->id = nextID; /* put int from next_id() into curr->id */
curr->next = NULL; /* terminate linked list */
}
printf("Enter originators name:\n");
scanf("%s", &curr->sender);
printf("Enter addressee name:\n");
scanf("%s", &curr->addressee);
printf("Enter Subject:\n");
scanf("%s\n", &curr->regarding);
printf("Enter the date (mm dd yy):\n");
scanf("%d %d &d\n", &curr->date.month, &curr->date.day, &curr->date.year);
printf("Enter letter file name:\n");
scanf("%s\n", &curr->fname);
printf("The new letter id # is %d", curr->id);
}