0

case 3 is the troubled zone, an integer array is working, it's fine, but "first" and "last" string arrays aren't working at all. I don't know how to solve it.

I have to use only stdio.h and stdlib.h libraries."first" and "last" are 2d char pointers, I'd like to 20 elements and maximum 30 characters.Adding ,and editing are fine it's working, but "first[temp]=first[temp+1];" this code cause compiler error when I try "strcpy(first[temp],first[temp+1]);" there is no compiler error.

Eventually using strcpy. when I want to delete the strings remain the same, and the integers take the value of 0

int main(){

    printf("Welcome to address book ! \n ");
    printf("--------------------------------------------------------\n");
    int a=0;
    int temp=0;
    char* first[20][30];
    char* last[20][30];
    int n[20];
    int g=0;

    while(a<=1000){
        int b=0;
        printf("What do you want? \n");
        printf("1-New ID\n2-Edit ID\n3-Delete ID\n4-List All ID's\n5-Close the Program\n");
        printf("--------------------------------------------------------\n");
        scanf("%d",&b);

        switch(b){
        case 1:
           printf("Enter Fist Name \n");
           scanf("%s",&first[g]);
           printf("Enter Last Name\n");
           scanf("%s",&last[g]);
           printf("Enter Student Number\n");
           scanf("%d",&n[g]);
           g++;
        a++;
        continue;
        case 2:
            printf("Which user you want to change ?\n");
            printf("user sequence :");
            scanf("%d",&temp);
            temp=temp-1;
            printf("Listed First Name : %s\n",first[temp]);
            printf("New First Name : ");
            scanf("%s",&first[temp]);

            printf("Listed Last Name : %s\n",last[temp]);
            printf("New Last Name : ");
            scanf("%s",&last[temp]);

            printf("Listed Number : %d\n",n[temp]);
            printf("New Listed Number : ");
            scanf("%d",&n[temp]);

            temp=0;
         a++;
        continue;
        case 3:
            printf("which user do you want to delete :");
            scanf("%d",&temp);
            if(temp+1<=g){
             //   first[temp]=first[temp+1];
            //  last[temp]=last[temp+1];
                n[temp]=n[temp+1];
            }
            else
                printf("unavailable");
        a++;
        continue;
        case 4:
           printf("--------------------------------------------------------\n");
           int u;
           for(u=0;u<g;u++){
            printf(">%d.user.  ",u+1);
            printf("%d\t",n[u]);
            printf("%s\t",first[u]);
            printf("%s\n",last[u]);
           }
           printf("\n--------------------------------------------------------\n");
          a++;
          continue;
        case 5:
            printf("\nThank you for the use this program !\n ");
        temp= 1000-a;
        a= a+temp;
        a++;
        continue;
        }
    }
    return 0;}
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Suat Alkan
  • 39
  • 6
  • stackoverflow didn't allow the whole code block, so I had to be send this code like this. There is no any loop or switch case mistakes, only problem is how to delete an element from string array in c. – Suat Alkan Nov 30 '17 at 10:52
  • "is not working" is not a problem description. You need to provide a [mcve] (emphasis on Minimal). – Jabberwocky Nov 30 '17 at 10:57
  • Please read and understand what is needed for a [MCVE]. There is a lot of missing information here, both in the code and in the problem description. `n[]` is used uninitialized in the code snippet; there are many other unexplained variables. `first` and `last` are 2d arrays of pointers; is this the intention? Even the question is unclear; an example of input and desired output would be helpful. – ad absurdum Nov 30 '17 at 10:57
  • [link](https://imgur.com/IfCPe91) whole code is here. – Suat Alkan Nov 30 '17 at 11:10
  • You need to add actual code to the post, not a link to an image of the code. That said, the code is quite wrong. Consider: `char* first[20][30];` is a 2d array of pointers. There is no space allocated for character storage. Then, in the linked image you have: `scanf("%s", &first[g]);`. This is completely wrong. [Get a good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn about pointers, arrays, and strings. – ad absurdum Nov 30 '17 at 11:21
  • Step 1 will likely be `char* first[20][30];` --> `char first[20][30];`. Step 2: heed all compiler warnings. – ad absurdum Nov 30 '17 at 11:28
  • @DavidBowling I added whole code block – Suat Alkan Nov 30 '17 at 11:40
  • When I compile this, even with only default warning levels, I see 8 warnings like: `warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char * (*)[30]’ [-Wformat=] scanf("%s",&first[g]);` – ad absurdum Nov 30 '17 at 11:45
  • 1
    Just because code appears to work does not mean that it is correct. In this case the code may appear to work under limited circumstances, but the program has undefined behavior, and so is invalid. Pay attention to the warnings and fix them. I would suggest compiling with additional warning flags always: `gcc -Wall -Wextra -Wpedantic`, for example. – ad absurdum Nov 30 '17 at 11:48
  • I turn char* first[20][30] to char first[20][20] and I tried to deletion as a result, names and last names are gone but other letters are coming instead, so the order is not shifting, when I delete the third user, the fourth user is not the new third user. – Suat Alkan Nov 30 '17 at 11:51
  • You say that you can only use `stdio.h` and `stdlib.h`. This means that you can't use `strcpy()`, which is in `string.h`. Your best bet is probably copy all characters from rows after the deleted row forward by one row, but without `strcpy()` you will have to write the copy code yourself. You could create arrays of pointers to names, and do pointer manipulation instead. A better solution would be to use an array of pointers to `struct`s where each `struct` element is a student record. But these last two would require a better understanding of pointers than seen in the posted code. – ad absurdum Nov 30 '17 at 12:16
  • You want the user to enter strings. Your program doesn't provide any storage for string. If you believe `first` and `last` are arrays of arrays or characters you're wrong. The are uninitialized pointers. Find a proper allocation of your data. After that you can check if it's still doesn't work. -- Hint: use a `char` instead of `char *`. The `char *` requires an assignment, e.g. from an allocation function like `malloc`. – harper Nov 30 '17 at 13:40

0 Answers0