Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How can i delete one student in my database?


ID NAME TC NO TEL NO ADRESS
15 A 2 5 YYY
25 B 2 6 YYY
35 C 3 7 YYY
45 D 4 8 YYY
55 .
65 .

WHen i enter the id : 15 15's line must be delete all above.

how can i do this ?

C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//Prototypes functions
void add_student();
void delete_student();
void modify_student();
void list_student();
//Global variables
char deleteid[12];
char updateid[12];
char data_tc_idno[12];
char data_id[13];
char data_adress[30];
char data_name[30];
char data_telno[12];
int deleteStatus[30];
int list_no;
FILE *cfPtr; //File pointer
typedef struct list{
        char tc_idno[12];
        char id[13];
        char name[30];
        char telno[13];
        char adress[30];
};
struct list list2014[30];

int main(void)
{
        char key_command;
        printf("Student Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
       printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");
        do{
                key_command = _getch();
                if (key_command == 'a' || key_command == 'A')
                        add_student();
                else if (key_command == 'd' || key_command == 'D')
                        delete_student();
                else if (key_command == 'l' || key_command == 'L')
                        list_student();
                else if (key_command == 'q' || key_command == 'Q')
                        exit(0);
        } while (key_command != 'q' || key_command != 'Q');

        system("pause");

        return 0;
}
/////////////////////////////////////////////////////////////////////////////////
void add_student()
{
    int n=0;


if((cfPtr = fopen("student.txt", "a+"))==NULL)
	printf("FILE COULD NOT BE OPENED\n");
else{
        system("cls");
        printf("\n\nNumber of students enrolled:%d\n\n", list_no);
        printf("This is %d.student's information.\n", list_no + 1);

        printf("ID No :");
        scanf("%s", list2014[list_no].id);
        printf("T.C ID No:");
        scanf("%s", list2014[list_no].tc_idno);
        printf("Name:");
        scanf("%s", list2014[list_no].name);
        printf("Address :");
        scanf("%s", list2014[list_no].adress);
        printf("Phone Number:");
        scanf("%s", list2014[list_no].telno);
        system("cls");
        printf("\n\n1 Student added...\n\n");

// File processing Append WRÝTE START
  for(n=0;n<1;n++){
        n++;
     fprintf(cfPtr, "%s %s %s %s %s\n", list2014[list_no].id, list2014[list_no].tc_idno,list2014[list_no].name,list2014[list_no].adress,list2014[list_no].telno);

  }
       fclose(cfPtr);
// File processing END
list_no += 1;
        printf("Student Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
        printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");

}
}
//////////////////////////////////////////////////////////////////////////
void delete_student()
{
    
    ????????????????????????????????????

        printf("\nStudent deleted...\n");
        printf("\nStudent Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
        printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");
}
/////////////////////////////////////////////////////////////////////////
void list_student()
{
        int i;
        char key_command;
        system("cls");


        printf("List of Students\n");
        printf("  Student id------Student T.C------Student Name------Student Adress------Student Tel no\n");

        if((cfPtr = fopen("student.txt", "r"))==NULL)
	printf("FILE COULD NOT BE OPENED\n");
else{
    fscanf( cfPtr, "%s%s%s%s%s", list2014[i].id, list2014[i].tc_idno, list2014[i].name,list2014[i].adress,list2014[i].telno);

          while(!feof(cfPtr)) //kayit list_nosi degismediginden
        {

            printf( "  %s%16s%17s%18s%20s\n", list2014[i].id, list2014[i].tc_idno, list2014[i].name,list2014[i].adress,list2014[i].telno);
            fscanf( cfPtr, "%s%s%s%s%s", list2014[i].id, list2014[i].tc_idno, list2014[i].name,list2014[i].adress,list2014[i].telno);
            list_no++;
        }

       fclose(cfPtr);
}
        printf("\n");
         printf("\nThere is %d student in data.\n\n", list_no);
        printf("[B] Go back to the MENU\n");
        printf("[Q] Quit\n");
        printf("Choose option: ");
        key_command = _getch();
        if(key_command=='b' || key_command=='B')
        {
        system("cls");
        printf("Student Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
        printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");
        }
         else if (key_command == 'q' || key_command == 'Q')
                        exit(0);
}


/////////////////////////////////////////////////////////////////////////


i mean this how can i write delete_student function please help ...
Posted
Updated 19-May-14 8:31am
v2
Comments
PIEBALDconsult 19-May-14 14:03pm    
0) Should be pretty basic
1) What have you tried.
2) Never delete -- mark as inactive ot similar
cvogt61457 19-May-14 14:04pm    
I love homework problems. Check out this website.
http://www.w3schools.com/SQL
[no name] 19-May-14 14:42pm    
You would need to replace your ????????... line with what you have tried.

More seriously, you need to go through your file, delete the student record and then re-write the file.
Member 10829664 19-May-14 14:50pm    
Okey . I must open student.txt in delete_student function.
I want to this
delete_function()
START
Enter student id:
Student id line found.
Student id line deleted.
END.

1 solution

0. initialize all global variables.

char deleteid[12] = {0};
char updateid[12] = {0};
char data_tc_idno[12] = {0};
char data_id[13] = {0};
char data_adress[30] = {0};
char data_name[30] = {0};
char data_telno[12] = {0};
int deleteStatus[30] = {0};
struct student list2014[30] = {0};


1. rename your
C++
typedef struct list
to
C++
struct student
. The name "list" is misleading. Something like "student" should be more clear to anyone reading your code. You reference the type as a struct, so the typedef does nothing for you.

2. load: read your whole student list "list2014" from a file; for each student, set deleteStatus[index] to some value to indicate that slot is loaded and valid. Call this once before printing menu.

3. print: print the full list to the console - from "list2014", not from a file. This function should skip student records marked as deleted (see "deleteStatus") or unused.

4. to delete a student, find the student's index in list2014 and use that index to set deleteStatus[index] to a value than means "deleted".

This code is very messy:

C++
key_command = _getch();
if (key_command == 'a' || key_command == 'A')
    add_student();
else if (key_command == 'd' || key_command == 'D')
    delete_student();
else if (key_command == 'l' || key_command == 'L')
    list_student();
else if (key_command == 'q' || key_command == 'Q')
    exit(0);


Use the toupper function to convert the letter to uppercase. This will simplify your if statements (even better: replace the if/else's with a switch statement).

C++
key_command = toupper( _getch() );
if ('A' == key_command)
    add_student();
else if ('D' == key_command)
    delete_student();
else if ('L' == key_command)
    list_student();
else if ('Q' == key_command)
    exit(0);


5. save: write your full student list "list2014" to a different file - call this before exit. This function should skip student records marked as deleted (see "deleteStatus") or unused.

6. as a debugging aid, add a 'S' save command to call the save function so you can see the effects of each operation.

Good luck.
 
Share this answer
 
Comments
Stefan_Lang 20-May-14 5:41am    
Excellent response. Even better for pointing out many of the weaknesses beyond the actual question! My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900