I have this program to add, edit, delete and view the student file, which is save as student.csv. The problem is that the only function that is working is the add and view. The edit and delete does not have problem on the code but it does not edit and delete on the "student.csv" which is the excel file. Can someone please help me regarding this problem? Please see my code.

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


    FILE *inputFile;
    FILE *stud_rec, *stud_rec2;
    FILE *inst_rec, *inst_rec2;
    FILE *subj_rec, *subj_rec2;
    FILE *class_rec, *class_rec2;
    char inputString[50], answer;
    char student[256],instructor[256],subject[256], class[256];
    char option[2];
    char studid[10], subj_name[30];
    char lastname[15], firstname[15], midname[15];
    char subj_code[6], inst_code[6];
    int choice;
    int stud_menu, inst_menu, subj_menu, enroll, class_list; 
    char stud_name, classlist;
    int linecount=0, counter;
    
typedef struct studentinfo
{
   char studid[10];
   char lastname[16], firstname[16];
   char midname[16];
   char stud_name;     
}studentinfo; 

typedef struct instructorinfoType
{
   char inst_code[5+1];
   char instructor_name[35+1];
}instructorinfoType; 

typedef struct subjectinfoType
{
   char subj_code[10+1];
   char subj_name[35+1];
   char inst_code[5+1];
}subjectinfoType; 

typedef struct classlistType
{
   char studid[10];
   char subj_code[10+1];
}classlistType;


    
    
void menu()
{
    printf("Main Menu\n");
    printf("1. Students Information\n");
    printf("2. Instructors Information\n");
    printf("3. Subjects Information\n");
    printf("4. Enrollment\n");
    printf("5. Class List\n");
    printf("0. Exit System\n");
    printf("Select number from the following options: ");
    scanf("%d", &choice);
    system("cls");    
}

void submenu()
{
         printf("1. View List\n");
         printf("2. Add\n");
         printf("3. Edit\n");
         printf("4. Delete\n");
         printf("0. Back to Main Menu\n");
         
}
    

void view_student()
{
                 system("cls");
                 inputFile = fopen("student.csv", "rt");
                 while(!feof(inputFile))
                 {
                 fscanf(inputFile,"%[^\n]\n",student);
                 printf("%s\n",student);
                 }
                 fclose(inputFile);
                 getchar();
}

void view_instructor()
{
                 system("cls");
                 inputFile = fopen("instructor.csv", "rt");
                 while(!feof(inputFile))
                 {
                 fscanf(inputFile,"%[^\n]\n",instructor);
                 printf("%s\n",instructor);
                 }
                 fclose(inputFile);
                 getchar();
}

void view_subject()
{
                 system("cls");
                 inputFile = fopen("subject.csv", "rt");
                 while(!feof(inputFile))
                 {
                 fscanf(inputFile,"%[^\n]\n",subject);
                 printf("%s\n",subject);
                 }
                 fclose(inputFile);
                 getchar();
}

void view_class()
{
                 system("cls");
                 inputFile = fopen("class.csv", "rt");
                 while(!feof(inputFile))
                 {
                 fscanf(inputFile,"%[^\n]\n",class);
                 printf("%s\n",class);
                 }
                 fclose(inputFile);
                 getchar();
}

void add_student()
{
     inputFile=(fopen("student.csv","at"));
     fprintf(inputFile,"%s, %s, %s, %s\n", studid, lastname, firstname, midname);
     fclose(inputFile);
     inputFile=fopen("student.csv","rt");
     rewind(inputFile);
     
     while (!feof(inputFile))
     {
           fscanf(inputFile, "%[^,], %[^,],%[^,],%[^\n]\n,",studid,lastname,firstname,
           midname);
           printf("%s, %s, %s, %s\n",studid,lastname,firstname,midname);
     }
     fclose(inputFile);
     
     
     
}

void add_instructor()
{
     inputFile=(fopen("instructor.csv","at"));
     fprintf(inputFile,"%s, %s, %s\n", inst_code,firstname,lastname);
     fclose(inputFile);
     inputFile=fopen("instructor.csv","rt");
     rewind(inputFile);
     
     while (!feof(inputFile))
     {
           fscanf(inputFile, "%[^,], %[^,] %[^\n]\n,",inst_code,firstname,lastname);
           printf("%s, %s, %s\n",inst_code,firstname,lastname);
     }
     fclose(inputFile);
     
}

void add_subject()
{
     inputFile=(fopen("subject.csv","at"));
     fprintf(inputFile,"%s, %s, %s\n", subj_code,subj_name,inst_code);
     fclose(inputFile);
     inputFile=fopen("subject.csv","rt");
     rewind(inputFile);
     
     while (!feof(inputFile))
     {
           fscanf(inputFile, "%[^,], %[^,] %[^\n]\n,", subj_code, subj_name, inst_code);
           printf("%s, %s, %s\n", subj_code, subj_name,inst_code);
     }
     fclose(inputFile);
     
}

void add_class()
{
     inputFile=(fopen("class.csv","at"));
     fprintf(inputFile,"%s, %s\n", studid, subj_code);
     fclose(inputFile);
     inputFile=fopen("class.csv","rt");
     rewind(inputFile);
     
     while (!feof(inputFile))
     {
           fscanf(inputFile, "%[^,], %[^\n]\n,", studid, subj_code);
           printf("%s, %s\n", studid, subj_code);
     }
     fclose(inputFile);
     
}

void switch_student()
{
      switch (stud_menu)
         {
         case 1:
                 view_student();
                 break;
                
    
         case 2:
              do{
              printf("\nEnter the student number <0000-0000>: ");
              scanf("%s",studid);
              printf("Enter Lastname: ");
              scanf("%s",lastname);
              printf("Enter Firstname: ");
              scanf("%s",firstname);
              printf("Enter Middlename: ");
              scanf("%s",midname);
              add_student();
              
              printf("Add another data? [y/n]: ");
              scanf("%s", option);
              
    
              if (strcmp(option,"y")==0) 
               {
                 add_student(); 
               }
              if (strcmp(option,"n")==0)
              {
                 printf("Good bye!!!");
              }
      
              else
              {
                  printf("Choice is not available!");
              }
              }while((strcmp(option,"y")==0));
              system("cls");
        break;
              
         case 3:
              printf("Enter student ID: ");
              scanf("%s",studid);
              break;
              
         case 4:
              printf("Enter student ID: ");
              scanf("%s",studid);
              
              
              break;
              
         case 0:
              menu();
              break;
         }
}

void switch_instructor()
{
     switch (inst_menu)
         {
         case 1:
                 view_instructor();
                 break;
    
         case 2:
              do{
              printf("Enter Instructor ID: ");
              scanf("%s",inst_code);
              printf("Enter Firstname: ");
              scanf("%s",firstname);
              printf("Enter Lastname: ");
              scanf("%s",lastname);
              add_instructor();
              
              printf("Add another data? [y/n]: ");
              scanf("%s", option);
              
    
              if (strcmp(option,"y")==0) 
                  {
                 add_instructor(); 
                  }
              if (strcmp(option,"n")==0)
                 {
                 printf("Good bye!!!");
                 }
      
              else
                 
                 printf("Choice is not available!");
                 }while(strcmp(option,"y")==0);
               
               system("cls");
               
              break;
              
          case 3:
              printf("Enter student ID: ");
              scanf("%s",studid);
              break;
              
         case 4:
              printf("Enter student ID: ");
              scanf("%s",studid);
              break;
              
         case 0:
              menu();
              break;
         }
}       


void switch_subj()
{
      switch (subj_menu)
         {
         case 1:
                 view_subject();
                 break;
    
         case 2:
              do{
              printf("Enter Subject Code: ");
              scanf("%s",subj_code);
              printf("Enter Subject Name: ");
              scanf("%s",subj_name);
              printf("Enter Instructor ID: ");
              scanf("%s",inst_code);
              add_subject();
              
              printf("Add another data? [y/n]: ");
              scanf("%s", option);
              
    
              if (strcmp(option,"y")==0) 
               {
                 add_subject(); 
               }
              if (strcmp(option,"n")==0)
              {
                 printf("Good bye!!!");
              }
      
              else
              
                  printf("Choice is not available!");
            
              }while((strcmp(option,"y")==0));
              system("cls");
              break;
              
          case 3:
              printf("Enter student ID: ");
              scanf("%s",studid);
              break;
              
         case 4:
              printf("Enter student ID: ");
              scanf("%s",studid);
              break;
              
         case 0:
              menu();
              break;
         }
}

void switch_enroll()
{
      switch (enroll)
         {
         case 1:
              do{
              printf("Enter Student ID: ");
              scanf("%s",inst_code);
              printf("Enter Firstname: ");
              scanf("%s",firstname);
              printf("Enter Lastname: ");
              scanf("%s",lastname);
              add_class();
              
              printf("Add another data? [y/n]: ");
              scanf("%s", option);
              
    
              if (strcmp(option,"y")==0) 
               {
                 add_student(); 
               }
              if (strcmp(option,"n")==0)
              {
                 printf("Good bye!!!");
              }
      
              else
              
                  printf("Choice is not available!");
              
              }while((strcmp(option,"y")==0));
              system("cls");
              break;
              
          case 2:
              printf("Enter student ID: ");
              scanf("%s",studid);
              break;
              
         case 0:
              menu();
              break;
         }
}

void switch_class()
{
     switch (classlist)
         {
         case 1:
                 view_class();
                 break;
                
    
         case 0:
              menu();
              break;
         }
        
      
         
}

void del_stud()
{
     
   int linecount=0, counter, linecountmax, flag = 0; ////
   char line[256]; ////
   char temp[50]; 
   char *answer;
   //studentinfo *student;
    
   stud_rec=fopen("student.csv", "rt");//openfile(); //studentfile=fopen("students.csv", "rt");

   while(!feof(stud_rec))
   {
   fscanf(stud_rec, "%[^\n]\n", line);
   linecount++;
   }
   
   linecountmax=linecount;
   
   rewind(stud_rec);
   
   studentinfo *student;
   student=malloc(linecountmax*(sizeof(studentinfo)));
    
  linecount=0;
   
   
  while(!feof(stud_rec))
   { 
   fscanf(stud_rec, "%[^,], %[^,], %[^,], %[^\n]\n", student[linecount].studid, 
   student[linecount].lastname, student[linecount].firstname, 
   student[linecount].midname);
   linecount++;
   linecountmax=linecount;
   }
    
   fclose(stud_rec);
     system("cls");
     view_instructor();
     printf("Enter Student ID to be delete:");
     scanf("%s",studid);
     counter = 0;
    
     
     
     while ((strcmp(studid,student[counter].studid) != 0) || (counter > linecount))
     {
       counter++;
     }
     if (counter <= linecount) flag = 1;
                 
     if (flag == 1)
     {
     printf("Are you sure you want to delete %s, %s\n", student[counter].studid,
     student[counter].lastname, student[counter].firstname, student[counter].midname);
     scanf("%s", answer);
     if(strcmp(answer,"Y")==0)
     {
     stud_rec2=fopen("student.csv", "wt");
     linecount=0;
     while(linecount!=linecountmax)
     {
     if (linecount!=counter)
     {
     fprintf(stud_rec2, "%s, %s\n", student[linecount].studid, 
     student[counter].lastname, student[counter].firstname, student[counter].midname);
     }
     linecount++;
     }   
     }   
    
     else if (strcmp(answer, "N")==0)
     {
     system("cls");
     view_student(); 
     }
     flag = 0;                
                 fclose(stud_rec2); 
                 printf("Record Deleted...\n\n");
                 system("pause");
                 system("cls");
                 main();

                 //viewstudentinfopro();
                 }
                 
                 
                 else 
                      {
                      system("cls");
                      main();//studentinfopro(); //balik sa menu ng stud info (or del sud)
                      }
                      
                      
                 
getch();
}

void edit_stud()
{
   int linecount=0, counter, linecountmax, flag = 0; ////
   char line[256]; ////
   char temp[50]; 
   char *answer;  
   
   
   system("cls");
                 stud_rec=fopen("student.csv", "rt");//openfile(); //studfile=fopen("students.csv", "rt");
                 //viewstudentinfopro(); //view file
                 rewind(stud_rec);
                 
                 counter=0; ////
                 linecount=0; ////
                 int count=0; ////
                 studentinfo *student;
                 
                 printf("\n\n");
                 
                 printf("Enter student number of the record to be edited:");
                 scanf("%s", studid);
                 system("cls");
                 while(counter<=linecountmax)
                 {
                       if(strcmp(studid,student[linecount].studid)!=0)
                          {  
                          linecount++;
                          counter++;
                          }
                          //else if(
                                                 
                       else 
                          {                                                
                          printf("\t\tEditing a record\t\t\n");
                          printf("%s, %s, %s, %s\n", studid, student[linecount].lastname,
                          student[linecount].firstname, student[linecount].midname);                          
                          printf("Edit this record?(Y/N)");
                          scanf("%s", answer);
                          if(strcmp(answer, "Y")==0)
                              {
                                 printf("Enter student's lastname<maximum of 15 characters only>:");
    			                 fflush(stdin);
                                 gets(student[linecount].lastname);
    			                 printf("Enter student's firstname<maximum of 15 characters only>:");
                                 gets(student[linecount].firstname);
                                 printf("Enter student's middlename:");
                                 gets(student[linecount].midname);
                                 
                                 stud_rec2=fopen("student.csv", "wt");
                                 counter=0;
                                    while(counter<linecountmax)
                                    {
                                      if (counter!=linecount)
                                         {
                                         fprintf(stud_rec2, "%s, %s, %s, %s\n", student[counter].studid, 
                                         student[counter].lastname, student[counter].firstname, student[counter].midname);
                                         
                                         }
                                      else 
                                      fprintf(stud_rec2, "%s, %s, %s, %s\n", student[linecount].studid,
                                      student[linecount].lastname, student[linecount].firstname,
                                      student[linecount].midname);
                                      counter++;                
                                    }
             
                                    fclose(stud_rec2);
                                    fclose(stud_rec);//closefile();//fclose(studfile);
                                    system("cls");
                                    printf("\t\tEditing a record\t\t\n");
                                    printf("New student record: %s, %s, %s, %s\n", student[linecount].studid,
                                    student[linecount].lastname, student[linecount].firstname,
                                    student[linecount].midname);
                                    system("pause");
                                    break;
                                   
                              }
                              else if(strcmp(answer, "N")==0)
                                   {
                                   system("cls");
                                   main();
                                   }
                              else
                              {
                              printf("%s is not found in the record.\n", studid);
                              system("pause");
                              system("cls");
                              main();//studentinfopro(); //balik sa menu ng stud info (or del sud)
                              }
                          }                                                                  
                 }
                   
                  
     if(counter>linecountmax)
     {             
     printf("Error. Student number %s not in the list.\n\n", studid);
     system("pause");
     system("cls");
     main();                
     }         
}



int main(void)
{
    
    
    menu();
    

   
    
    switch (choice)
    {
    case 1:
         printf("Students Information Menu: \n");
         submenu();
         printf("Select your choice: ");
         scanf("%d", &stud_menu);
         system("cls");
         switch_student();
        
      
         break;
         
    
    case 2:
         printf("Instructors Information Menu: \n");
         submenu();
         printf("Select your choice: ");
         scanf("%d", &inst_menu);
         system("cls");
         switch_instructor();
         break;
         
       
         
    case 3:
         printf("Subjects Information Menu: \n");
         submenu();
         printf("Select your choice: ");
         scanf("%d", &subj_menu);
         system("cls");
         switch_subj(); 
        
         break;
         
    case 4:
         printf("Enrollment Menu: \n");
         printf("1. Add\n");
         printf("2. Delete\n");
         printf("0. Back to Main Menu\n");
         printf("Select your choice: ");
         scanf("%d", &enroll);
         system("cls");
         switch_enroll();
         
         break;
         
         
         
    case 5:
         printf("Class List Menu: \n");
         printf("1. View List\n");
         printf("0. Back to Main Menu\n");
         printf("Select your choice: ");
         scanf("%d", &class_list);
         system("cls");
         switch_class();
        
         
    case 0:
         printf("Exit");
         break;
    
    default:
         printf("Choice is not available");
         break;   

  }
            
   
    
    
    getch();
    
    
}

Youre very fast response will be appreciated. Thanks...

Recommended Answers

All 4 Replies

I read your code. It has been very confusing. Firstly your code should be structured, so, all your functions should take parameters. If you want to use on the global scope, it would be hard. Secondly, you have to write one function for one target, For example , you can write search funciton that returns number of student position. So it would be reuse. you have not to search for each function. Last one you can code while loop for looking menu when you want on the screen... I hope to help you..

Line 571 has this code:

printf("Edit this record?(Y/N)");
scanf("%s", answer);
if(strcmp(answer, "Y")==0)

Which I believe should be changed to:

scanf("%c", &answer);  //answer is a char, not a string
if(answer == 'Y')      //note the change to a single quote for a char, instead of double quotes
   etc.

You have the same problem with the variable "answer" on line 491 in your student delete function.

Line 571 has this code:

printf("Edit this record?(Y/N)");
scanf("%s", answer);
if(strcmp(answer, "Y")==0)

Which I believe should be changed to:

scanf("%c", &answer);  //answer is a char, not a string
if(answer == 'Y')      //note the change to a single quote for a char, instead of double quotes
   etc.

You have the same problem with the variable "answer" on line 491 in your student delete function.

In an ideal scenario where the user enters only the expected input, both situation will do the same thing. But as it stands there are a lot of pitfalls breaking the program.
Little example: 539. char *answer; Where's answer pointing to?
scanf() needs a buffer set with the proper size of memory to receive the inputed answer.


>Can someone please help me regarding this problem? Please see my code.
Test every function in a separated environment. As it stand you are asking someone to debug your code, and that's not help at all.
If you don't know what I am taking about read on about a development process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.