S15_88
March 20th, 2008, 02:25 AM
hi i'm wondering why i'm getting a segfault before the user can input data. I had the program running fine earlier before i tried to make a list. Just wondering if i'm doing something wrong with respect to the pointers. If anyone could give me some tips on how to make a list and stuff that would be great as i'm pretty confused right now.
i'm guessing that it has something to do with setting the pointers to NULL?
Thanks, ALain
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 250
struct node
{
char *name;
int age;
struct node *nextName;
struct node *nextAge;
};
typedef struct node NODE;
int drain_stdin()
{
int c;
while((c = getchar()) != '\n' && c != EOF );
return c;
}
void addStruct(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF)
{
printf("Enter name: ");
fgets(ptr->name, MAX_LEN + 1, stdin);
printf("Enter age: ");
scanf("%d", &ptr->age);
ptr->nextName = root;
ptr->nextAge = root;
root = ptr;
}
else
{
printf("An Error Has Occured\n");
}
}
void removeStruct(NODE *root, NODE *ptr)
{
char nameRemove[MAX_LEN + 1];
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF )
{
printf("Enter Name Of Desired Record To Be Removed: ");
fgets(nameRemove, MAX_LEN + 1, stdin);
if(strcmp(nameRemove, ptr->name) == 0)
{
printf("Found The Record\n");
}
else
{
printf("There Is No Record With That Name\n");
}
}
else
{
printf("An Error Has Occured\n");
}
}
void printName(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void printAge(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void progExit(NODE *root, NODE *ptr)
{
}
int main()
{
int choice;
NODE *root, *ptr;
ptr = malloc(sizeof(NODE));
ptr->name = malloc(sizeof(char)*MAX_LEN);
do
{
printf("1. Add structure\n");
printf("2. Remove structure\n");
printf("3. Print names\n");
printf("4. Print ages\n");
printf("5. Exit\n");
scanf("%d", &choice);
if(choice == 1)
{
addStruct(root, ptr);
}
else if(choice == 2)
{
removeStruct(root, ptr);
}
else if(choice == 3)
{
printName(root, ptr);
}
else if(choice == 4)
{
printAge(root, ptr);
}
else if(choice == 5)
{
progExit(root, ptr);
}
else
{
return 0;
}
}while(choice != 5);
return 0;
}
i'm guessing that it has something to do with setting the pointers to NULL?
Thanks, ALain
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 250
struct node
{
char *name;
int age;
struct node *nextName;
struct node *nextAge;
};
typedef struct node NODE;
int drain_stdin()
{
int c;
while((c = getchar()) != '\n' && c != EOF );
return c;
}
void addStruct(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF)
{
printf("Enter name: ");
fgets(ptr->name, MAX_LEN + 1, stdin);
printf("Enter age: ");
scanf("%d", &ptr->age);
ptr->nextName = root;
ptr->nextAge = root;
root = ptr;
}
else
{
printf("An Error Has Occured\n");
}
}
void removeStruct(NODE *root, NODE *ptr)
{
char nameRemove[MAX_LEN + 1];
root = NULL;
ptr = NULL;
if (drain_stdin() != EOF )
{
printf("Enter Name Of Desired Record To Be Removed: ");
fgets(nameRemove, MAX_LEN + 1, stdin);
if(strcmp(nameRemove, ptr->name) == 0)
{
printf("Found The Record\n");
}
else
{
printf("There Is No Record With That Name\n");
}
}
else
{
printf("An Error Has Occured\n");
}
}
void printName(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void printAge(NODE *root, NODE *ptr)
{
root = NULL;
ptr = NULL;
printf("%s %d\n", ptr->name, ptr->age);
}
void progExit(NODE *root, NODE *ptr)
{
}
int main()
{
int choice;
NODE *root, *ptr;
ptr = malloc(sizeof(NODE));
ptr->name = malloc(sizeof(char)*MAX_LEN);
do
{
printf("1. Add structure\n");
printf("2. Remove structure\n");
printf("3. Print names\n");
printf("4. Print ages\n");
printf("5. Exit\n");
scanf("%d", &choice);
if(choice == 1)
{
addStruct(root, ptr);
}
else if(choice == 2)
{
removeStruct(root, ptr);
}
else if(choice == 3)
{
printName(root, ptr);
}
else if(choice == 4)
{
printAge(root, ptr);
}
else if(choice == 5)
{
progExit(root, ptr);
}
else
{
return 0;
}
}while(choice != 5);
return 0;
}