Add를 먼저 하고 다른것들 하면 문제없이 돌아가는데 다른것들 먼저하면 저런식으로 프로그램이 팅기네요 문제가 뭔지 모르겠습니다 형들 한번만 도와주십셔
소스코드도 올리겠습니다
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student //linked list
{
char name[30];
char ID_num[30];
char major[30];
struct student *next;
};
struct student *first = NULL;
struct student *current = NULL;
struct student *previous = NULL;
void ADD(); //prototype
void DELETE();
void EDIT();
void PRINT_ALL();
void SEARCH();
FILE *pfile = NULL; //file pointer
/* 메인 함수 */
int main (void)
{
pfile = fopen("student.txt", "r"); //파일에서 데이터 읽어옴
for( ; ; )
{
current = (struct student*) malloc(sizeof(struct student));
if(first == NULL)
first = current;
if(previous != NULL)
previous->next = current;
if(fscanf(pfile, "%s", current->name) == EOF)
break;
fscanf(pfile, "%s", current->ID_num);
fscanf(pfile, "%s", current->major);
current->next = NULL;
previous = current;
}
fclose(pfile);
int select = 0;
printf(">> Student Management System << ");
for( ; ; )
{
printf("========Menu======== "); //메뉴 선택
printf("1. Add "
"2. Delete "
"3. Modify "
"4. Search "
"5. Display all "
"6. End program ");
printf("==================== ");
printf("Select a function >> ");
scanf("%d", &select);
switch (select)
{
case 1:
ADD();
break;
case 2:
DELETE();
break;
case 3:
EDIT();
break;
case 4:
SEARCH();
break;
case 5:
PRINT_ALL();
break;
case 6:
return 0;
default :
printf("incorrect input");
break;
}
}
return 0;
}
/* 함수 정의 */
void ADD(void)
{
pfile = fopen("student.txt", "a");
current = (struct student*) malloc(sizeof(struct student));
if(first == NULL)
first = current;
if(previous != NULL)
previous->next = current;
printf("Enter name: ");
scanf("%s", current->name);
fprintf(pfile, "%s ", current->name);
printf("Enter ID number: ");
scanf("%s", current->ID_num);
fprintf(pfile, "%s ", current->ID_num);
printf("Enter major: ");
scanf("%s", current->major);
fprintf(pfile, "%s ", current->major);
current->next = NULL;
previous = current;
fclose(pfile);
printf("Add completed! ");
}
void DELETE(void)
{
char name_del[30];
printf("Enter the name of the student you want to delete: ");
scanf("%s", name_del);
current = first;
while(current != NULL)
{
if(strcmp(name_del,current->name) == 0)
{
if(previous == NULL)
first = current->next;
else
{
previous->next = current->next;
free(current);
current = previous->next;
}
}
previous = current;
current = previous->next;
}
pfile = fopen("student.txt", "w");
current = first;
while(current->name != NULL)
{
fprintf(pfile, "%s %s %s ", current->name, current->ID_num, current->major);
current = current->next;
}
previous = current;
fclose(pfile);
printf("Delete completed! ");
}
void EDIT(void)
{
char name_edit[30];
printf("Enter the name of the student you want to modify: ");
scanf("%s", name_edit);
current = first;
while(current != NULL)
{
if(strcmp(name_edit, current->name) == 0)
{
printf("Enter new name: ");
scanf("%s", current->name);
printf("Enter new ID number: ");
scanf("%s", current->ID_num);
printf("Enter new major: ");
scanf("%s", current->major);
}
previous = current;
current = previous->next;
}
pfile = fopen("student.txt", "w");
current = first;
while(current->name != NULL)
{
fprintf(pfile, "%s %s %s ", current->name, current->ID_num, current->major);
current = current->next;
}
previous = current;
fclose(pfile);
}
void SEARCH(void)
{
char name_search[30];
printf("Enter the name of student you want to search: ");
scanf("%s", name_search);
current = first;
while(current != NULL)
{
if(strcmp(name_search, current->name) == 0)
{
printf("%s %s %s ", current->name, current->ID_num, current->major);
}
previous = current;
current = previous->next;
}
}
void PRINT_ALL(void)
{
current = first;
while(current != NULL)
{
printf("%s %s %s ", current->name, current->ID_num, current->major);
previous = current;
current = previous->next;
}
}
제발 한번만 도와주세요 형들
F10
뭐하는 프로그램인데
연결리스트랑 파일입출력 이용해서 학생자료 추가하고 지우고 검색하는 프로그램입니다
dev C++ 문제입니다
헉 그래요? 다른 프로그램써야하나요?
current->name
아니 딱 봐도 널 참조 일어나잖아
형 좀만 풀어서 설명해주세요 ㅠㅠ 아직 2개월차 프알못이라 이해가 잘 안되네요