How to edit data from a Struct using a function

Asked

Viewed 1,497 times

0

I want to create an edit function that takes as a parameter the music array by reference. (using pointers)

The user must choose the song number and type back the data from that vector position.

I created the struct, I’m already receiving the values and I’m playing. But I don’t know how to edit the values. Someone 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(){}

1 answer

1


A few remarks before we begin:

  • Do not set the function after the main without first having the settings/signatures
  • If its structure is already global, it has been declared above the main, then you don’t need to pass it to any function, because you can access it directly. If you look carefully, this is what happens in the function play.
  • Do not include more headers that the strictly necessary.
  • Avoid using operating system-dependent functions such as getch

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

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

Which will now give you build error in function play. To correct, simply change the function to receive the vector as you want to do in the function editar:

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

And now when you call the function, you pass the vector address which is just music:

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

Now for the edit is identical, but passing 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’re making with fgets let the \n inside the strings, which is why you don’t have to do it manually. I’ve answered 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);
}
  • Your explanation was perfect. I understood all the steps in a succinct way. Thank you so much for your time and for your explanation, helped me too.

  • @spw No problem. We are here to help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.