“Database” in c (not actual database, just a simple thing) [closed]

The main idea of the code below is to let me add, delete etc portions of the arrays so that it looks more or less like a store’s database (did this because I have no idea how to use pointers and etc). I got the thing almost figured it out, but i am having 2 big problems: The first problem is that I get a “segmentation fault (core dumped)” when I run the code and select case 1. I don’t even know what the error means so I don’t see how can I fix it. The second problem which is probably my biggest, can anybody tell me how can I store the arrays in a file in such a way that when I run the program again, the arrays won’t be empty, but will take their elements from a file, and save in the same file when I exit (that’s why I used the word database).

include stdio.h
include stdbool.h

int main()

{ bool bExit = false;
  int select, i, price[5], amount[5];
  char *name[5];

do

{ printf("What would you like to do ?:n");
  printf("1) Add products n");
  printf("2) Delete products n");
  printf("3) Find product by name (shows position and price) n");
  printf("4) Find product by position (shows name and price) n");
  printf("6) Display all info of all products  n");
  printf("7) Modify amount of existing product n");
  printf("8) Modify price of existing product n");
  printf("9 Exit n");
  scanf("%d", &select);
  switch (select)
    {
    case 1:
      {
        for(i=1;i<=5;i++)
          {
            printf("Name: ");
            scanf("%cn", *name[i]);
            printf("Price: ");
            scanf("%dn", price[i]);
            printf("Amount: ");
            scanf("%dn", amount[i]);
          }
        break;
      }
    case 2:
    

… bla bla bla …

    default:
      {
        printf("Good Bye n");
        bExit = true;
        break;
      }

Answer

You do not need to use any pointer for char *name[5]. Use char name[5] instead. Probably the segmentation fault comes from this statement

scanf("%cn", *name[i]);

for the second question create a file and open it whenever you want to read/write. Something like

File *fptr=fopen("database","w");
for(i=0;i<5;i++){
    fprintf(fptr,"%sn",name[i]);

}

take a look in the link here:http://www.programiz.com/c-programming/c-file-input-output

Leave a Reply

Your email address will not be published. Required fields are marked *