How to edit data in a Struct using a function

0

I want to create an edit function that receives as a parameter by reference the vector of songs. (using pointers)

The user must choose the number of the song and re-enter the data of that position of the vector.

I created the struct, I'm already receiving the values and I'm playing. But I do not know how to edit the values. Anyone to help me start this part?

    #include <stdio.h>
    #include <stdlib.h> //
    #include <stdbool.h>
    #include <locale.h>
    #include <string.h>

      struct registry_of_music  {
        char name[50];
        char artist[60];
        char url[80];
    };
    struct registry_of_music music[9];

    int main() {
    int i;
    printf("\nRegistry of Music\n\n\n");

        for(i = 0; i <= 3;i++ ){
              printf("Name of Music: ");
              fflush(stdin);
              fgets(music[i].name, 50, stdin);

              printf("Name of Artist: ");
              fflush(stdin);
              fgets(music[i].artist, 60, stdin);

              printf("URL of Internet: ");
              fflush(stdin);
              fgets(music[i].url, 80, stdin);
        }

        int op;
        do
        {
            printf("1 - Play\n");
            printf("2 - Edit\n");
            printf("3 - Exit\n");
            printf("Please enter a value:");
            scanf("%d", &op);
                    switch(op) {
                case 1: play();
                        break;
                case 2: edit();
                        break;
                case 3: printf("Bye\n");
                        break;
                default: printf("Try Again\n");
            }
        } while (op!=3);

      getch();
      return(0);
}
    void play(){
    int i;
                    for(i = 0; i <= 3;i++ ){
                      printf("Name ...........: %s", music[i].name);
                      printf("Artist .....: %s", music[i].artist);
                      printf("URL .....: %s", music[i].url);
                    }
}

    void edit(){}
    
asked by anonymous 08.07.2018 / 11:14

1 answer

1

A few notes to get you started:

  • Do not put the functions after main without having the definitions / signatures
  • If your structure is already global, since it was declared above main , then you do not need to change it to any function, since you can access it directly. If you look closely it is what happens in the play function.
  • Do not include more headers than are strictly necessary.
  • Avoid using operating system dependent functions such as getch

So, the first step is to change the vector so that it is not global:

int main() {
    struct registry_of_music music[9];
    ...

That will now give you a compile error in the play function. To correct, just change the function to receive the vector as you want to do in the editar function:

void play(struct registry_of_music *music /*<--aqui*/) {
    //código da função
}

And now when calling the function, it passes the vector address that is just music :

int main() {
    ...
        switch(op) {
        case 1:
            play(music);
//                  ^---
            break;

Now to edit it is identical, but changing the position of the song to change:

case 2:
    printf("Please enter the position of the music to edit:");
    int edit_index;
    scanf("%d", &edit_index);
    edit(music, edit_index);
    //              ^----
    break;

And the function would look like this:

void edit(struct registry_of_music *music, int index) {
    printf("Editing music %s", music[index].name);
    printf("New Name: ");
    fflush(stdin);
    fgets(music[index].name, 50, stdin);

    printf("New Artist: ");
    fflush(stdin);
    fgets(music[index].artist, 60, stdin);

    printf("New URL: ");
    fflush(stdin);
    fgets(music[index].url, 80, stdin);
}

As a final note, the readings you are doing with fgets leave \n within strings, which is why you do not have to do it manually. I've already replied on how to get around this in other questions, such as in this .

Code with all changes for reference:

#include <stdio.h>

struct registry_of_music  {
    char name[50];
    char artist[60];
    char url[80];
};

void play(struct registry_of_music *music) {
    int i;
    for(i = 0; i <= 3; i++ ) {
        printf("Name ...........: %s", music[i].name);
        printf("Artist .....: %s", music[i].artist);
        printf("URL .....: %s", music[i].url);
    }
}

void edit(struct registry_of_music *music, int index) {
    printf("Editing music %s", music[index].name);
    printf("New Name: ");
    fflush(stdin);
    fgets(music[index].name, 50, stdin);

    printf("New Artist: ");
    fflush(stdin);
    fgets(music[index].artist, 60, stdin);

    printf("New URL: ");
    fflush(stdin);
    fgets(music[index].url, 80, stdin);
}

int main() {
    struct registry_of_music music[9];
    int i;
    printf("\nRegistry of Music\n\n\n");

    for(i = 0; i <= 3; i++ ) {
        printf("Name of Music: ");
        fflush(stdin);
        fgets(music[i].name, 50, stdin);

        printf("Name of Artist: ");
        fflush(stdin);
        fgets(music[i].artist, 60, stdin);

        printf("URL of Internet: ");
        fflush(stdin);
        fgets(music[i].url, 80, stdin);
    }

    int op;
    do {
        printf("1 - Play\n");
        printf("2 - Edit\n");
        printf("3 - Exit\n");
        printf("Please enter a value:");
        scanf("%d", &op);
        switch(op) {
        case 1:
            play(music);
            break;
        case 2:
            printf("Please enter the position of the music to edit:");
            int edit_index;
            scanf("%d", &edit_index);
            edit(music, edit_index);
            break;
        case 3:
            printf("Bye\n");
            break;
        default:
            printf("Try Again\n");
        }
    } while (op!=3);

    return(0);
}
    
08.07.2018 / 13:44