Can someone help me with my code? This is not actually my code but is a modified code from the internet. I need to make it work. Can someone point out where the problems are? Thanks.
#include<stdio.h> #include<string.h> #include<stdlib.h> FILE *filename; typedef struct student_record{ char firstname[255], middlename[255], lastname[255], course[255], email[255], contactnum[255], studentnum[255]; struct student_record *next; }studentdirectory; studentdirectory *start, *last; studentdirectory *e; // MENU ************************************************************************************************************ void menu(void) { printf("\n********************************************\n"); printf(" MENU \n"); printf("********************************************\n\n"); printf(" [1] Add new Record \n"); printf(" [2] Delete a Record \n"); printf(" [3] Search for a Record \n"); printf(" [4] Display all Records \n"); printf(" [5] Save to a File \n"); printf(" [6] Exit \n"); printf("\n********************************************\n"); } // ADD A RECORD ***************************************************************************************************** void add_new_record(studentdirectory *f){ f = (studentdirectory *)malloc(sizeof(studentdirectory)); printf("\nINPUT STUDENT INFORMATION:\n"); printf("\n\tInput student's last name: "); getchar(); scanf("%*c%[^\n]",f->lastname); printf("\n\tInput student's first name: "); getchar(); scanf("%*c%[^\n]",f->firstname); printf("\n\tInput student's middle name: "); getchar(); scanf("%*c%[^\n]",f->middlename); printf("\n\tInput %s %s's student number: ",f->firstname,f->lastname); getchar(); scanf("%*c%[^\n]",f->studentnum); printf("\n\tInput %s %s's course: ",f->firstname,f->lastname); getchar(); scanf("%*c%[^\n]",f->course); printf("\n\tInput %s %s's contact number: ",f->firstname,f->lastname); getchar(); scanf("%*c%[^\n]",f->contactnum); printf("\n\tInput %s %s's email address: ",f->firstname,f->lastname); getchar(); scanf("%*c%[^\n]",f->email); f = f->next; } // DELETE A RECORD ************************************************************************************************** void delete_a_record(void){ if(start==NULL){ printf("NO RECORD TO DELETE."); return; } studentdirectory *c, *d, *scan; char name[255]; printf("\nENTER LAST NAME TO DELETE: "); getchar(); scanf("%*c%[^\n]", name); last->next = NULL; c = start; scan = start; // IF THERE IS NO RECORD FOUND ********************************** while(scan!=NULL){ if(strcmp(name,scan->studentnum)==0){ break; } else{ if(scan->next==NULL){ printf("\n\nNO RECORD TO DELETE.\n\n"); free(scan); return; } scan=scan->next; } } // IF THERE IS A RECORD ***************************************** //IF RECORD IS AT THE BEGGINING if(strcmp(name,start->lastname)==0){ c = start; start = start->next; free(c); } //IF RECORD IS NOT AT THE BEGINNING else{ //IF RECORD IS AT THE END if (strcmp(name,last->lastname)==0){ c = start; while (c!=NULL){ if (c->next==last) //THIS FINDS THE LAST NODE IN THE LINKED LIST break; else c = c->next; // IF NOT YET FOUND, THIS WILL MOVE THE TEMP POINTER TO THE NEXT NODE } free(c->next); //AFTER FINDING THE LAST NODE, THIS WILL FREE THAT NODE last = c; //THEN THE 2ND TO THE LAST NODE WILL BE THE LAST NODE last->next = NULL; } //IF RECORD IS AT THE MIDDLE else{ last->next=NULL; c = start; //THIS WILL START FINDING THE RECORD IN THE MIDDLE while(strcmp(name,c->lastname)!=0){ c = c->next; } d = c; // AFTER FINDING THE RECORD TO DELETE, ANOTHER TEMP POINTER IS ASSIGNED c = c->next; // MOVE THE FIRST TEMPORARY POINTER TO THE NEXT NODE free(d); // FREE THE 2ND POINTER } } printf("\n\nRECORD DELETED.\n\n"); } // SEARCH FOR A RECORD ********************************************************************************************** void search_for_a_record(void){ studentdirectory *i; char name[255]; int indicator = 0; i = start; last->next = NULL; printf("\nENTER LASTNAME TO SEARCH (CASE SENSITIVE): \n"); getchar(); scanf("%[^\n]s",name); while(i!=NULL){ if (strcmp(name,i->lastname)==0){ printf("SEARCH RESULTS:\n"); printf("\n\tNAME: %s, %s %s", i->lastname,i->firstname,i->middlename); printf("\n\tSTUDENT NO: %s", i->studentnum); printf("\n\tCOURSE: %s",i->course); printf("\n\tCONTACT #: %s",i->contactnum); printf("\n\tE-MAIL ADDRESS: %s",i->email); indicator++; } i = i->next; } if(indicator==0){ printf("SEARCH RESULTS:\n"); printf("\nNO MATCHES FOUND.\n"); } } // DISPLAY ALL RECORDS ********************************************************************************************** void display_all_records(void){ studentdirectory *b; b = start; last->next=NULL; while (b!=last->next){ printf("\n*********************************************\n\n"); printf("Lastname: %s\n", b->lastname); printf("Firstname: %s\n", b->firstname); printf("Middlename: %s\n", b->middlename); printf("Student number: %s\n", b->studentnum); printf("Course: %s\n", b->course); printf("Contact Number: %s\n", b->contactnum); printf("E-mail: %s\n", b->email); b = b->next; } } // SAVE THE RECORD ************************************************************************************************** void save_to_a_file(void){ studentdirectory *a; a = start; // PUT TEMPORARY POINTER A TO START last->next=NULL; filename = fopen("Directory.txt","w+"); // OPEN THE FILE while(a!=NULL){ // LINKED LIST WILL CONTINUE TO BE SAVED TO THE FILE UNTIL END OF LINKED LIST IS REACHED fprintf(filename,"%s",a->firstname); fprintf(filename,"%s",a->middlename); fprintf(filename,"%s",a->lastname); fprintf(filename,"%s",a->studentnum); fprintf(filename,"%s",a->course); fprintf(filename,"%s",a->contactnum); fprintf(filename,"%s",a->email); a = a->next; } fclose(filename); // CLOSE THE FILE } // START OF MAIN **************************************************************************************************** main(){ int choice; studentdirectory *current; current = (studentdirectory *)malloc(sizeof(studentdirectory)); start=NULL; last=NULL; filename = fopen("Directory.txt","r"); if(filename!=NULL){ while(feof(filename)==0){ fgets(current->firstname,255,filename); fgets(current->middlename,255,filename); fgets(current->lastname,255,filename); fgets(current->studentnum,255,filename); fgets(current->course,255,filename); fgets(current->contactnum,255,filename); fgets(current->email,255,filename); if(start==NULL){ start = current; last = current; start->next = last; } else{ last->next = current; last = current; } } } // *************************************START OF SHOWN CONTENTS AT CONSOLE.************************************* e = start; do{ menu(); printf("\nYour choice: "); scanf("%d", &choice); switch(choice){ case 1: add_new_record(e); break; case 2: if(start!=NULL) delete_a_record(); else printf("No records to delete.\n"); break; case 3: if(start!=NULL) search_for_a_record(); else printf("No records to search.\n"); break; case 4: if(start!=NULL) display_all_records(); else printf("No records to display.\n"); break; case 5: save_to_a_file(); printf("The record has been saved.\n"); break; case 6: break; default:printf("Choice not found. Please select a number from the list above only."); break; } }while(choice!=6); }
please use sites like the following to paste your code.: http://pastebin.com/ http://codeviewer.org/upload gl!:)
Join our real-time social learning platform and learn together with your friends!