0

I have a simple register that requires the user to input a person's first name, last name, age, address and nickname. The assignment is to delete a users record with one option and to delete a single line in another. I'm not sure how to go about the delete.

I have created the general program and I know that it requires an IF statement for the two options but I'm not certain how to go about it.

#include <stdio.h>

int main(){
    int option;

      printf("[1] To create a new student onto the register\n");
      printf("[2] To delete a record in the register\n");
      printf("[3] To delete a line in the register\n");
      printf("Option: ");
      scanf("%d",option);

      if (option==1){
        Register();
      }
      if (option==2){

      }
      if (option==3){

      }
      return 0;
  }

void Register(){

    char Name[100],Address[100],Lname[100],Nickname[100];
    int Age=0,Phoneno=0;
    FILE*fptr;

    fptr = fopen("Register.txt", "a+");

    if(fptr == NULL)
    {
        printf("Error! There is no file to write to. Please Create a file");
    }

    fflush(stdin);

    printf("Enter the first name of a person: ");
    gets(Name);

    printf("Last Name of %s: ",Name);
    gets(Lname);

    printf("What is the age of %s: ",Name);
    scanf("%d", &Age);

    fflush(stdin);
    printf("What is the address of %s: ",Name);
    gets(Address);

    printf("Does %s have a nickname?");
    gets(Nickname);

    printf("This ends the entry of info into the file\n");

    fprintf(fptr,"First name: %s\n",Name);
    fprintf(fptr,"Last name: %s\n"Lname);
    fprintf(fptr,"Age: %d\n",Age);
    fprintf(fptr,"Address: %s\n",Address);
    fprintf(fptr,"Nickname:%s\n\n",Nickname);
    fclose(fptr);
}
  • If you choose option 1, the program creates a record of a student.
  • If you choose option 2, the program should delete all information about a person if you enter their first name.
  • If you choose option 3, the program asks for the students first name then asks which option to delete.

An example for option 3 is

First name: Michael
Last name:
Age:16
Address: 17 Habour street
Nickname: Mike

where his last name was erased

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Sure you don't need a database? – sjsam Apr 23 '19 at 03:55
  • 1
    im not too 100 on it, but i could add one i suppose – user11397064 Apr 23 '19 at 03:56
  • Is the file expected to be in plain text? – Jonathon K Apr 23 '19 at 04:00
  • yes, its a simple assignment rather than an offical register and from precious assignments we've used Notepad for all file related assignments – user11397064 Apr 23 '19 at 04:04
  • 2
    See other questions: (1) [Why `gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used); (2) [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer); (3) [Using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/using-fflushstdin). – Jonathan Leffler Apr 23 '19 at 06:10
  • You show the 'add' code; how do you add a second student? You don't show what you've tried for the delete code — what have you done? What is your plan of attack? (Hint: deleting from the middle of a file is hard; you normally create a new file with the relevant information missing, and maybe copy or move the new file so it replaces the old when you're done.) . How is your input code going to handle missing data from records in a file? What is the critical minimum set of fields? – Jonathan Leffler Apr 23 '19 at 06:12
  • 1
    `scanf("%d",option);` is a type error. Also, never use `scanf` without checking its return value. – melpomene Apr 23 '19 at 06:21
  • The easiest way to go about this would probably be to read the entire register file into a data structure, manipulate the data structure, then write the register back to a file. This way you avoid doing things the efficient/difficult way. – Indiana Kernick Apr 23 '19 at 06:47
  • The most common beginning data structures for something like this being either an *array of struct* (requires moving data to replace deleted records) or more appropriately a *linked-list* which allows adding and removing nodes (records) by simply changing where the pointers linking the nodes together point. You can do this with a with flat-text, but it too will require keeping track of and moving lines. Probably easiest would would be to create a struct to hold each line (and a flag indicating if the record is "visible") Then you can just output `if (visible == 1)`, etc.. – David C. Rankin Apr 23 '19 at 07:33

0 Answers0