Problems with CRUD in C language

Asked

Viewed 1,110 times

-1

Speak people, I need to do a CRUD with these characteristics there and I’m not getting, can you give me a light at the end of the tunnel there? thank you in advance.

My problem is not in storage, but in order, I do not know what is wrong, if anyone can help me I will be very grateful.

Make a C-language algorithm that emulates the characteristics of a music player running in text mode, via command prompt.

  1. You must create a playlist of the songs using a chained list. The chained list may be single or double, circular or not circular. It is up to the student to decide.

  2. The name of each song, artist/band and track length should be stored. For storage use a heterogeneous data structure.

  3. To enter the data, you can create a reading of the data through a menu on the screen or already leave them stored in a text file on your computer and only load this file when running the program. Or both solutions. Also decide how you will implement the insertion (at the beginning, at the end or in the middle of the chained list);

  4. There should be a menu on the screen. This menu should allow the insertion of new songs (in case of manual data insertion), you should have the option to list all the songs from the playlist (list of a chained list) on the screen and close the program;

this is my code :

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
struct Play_list
{  
    char musica[30];
    char Artista[30];
    float duracao;
    struct Play_list *proximo;
};
    struct Play_list * cria(void)
    {  
        return NULL;
    }

struct Play_list * insere(struct Play_list * l, char * musica, char * Artista, float duracao) { 
    struct Play_list * novo = (struct Play_list *) malloc(sizeof(struct Play_list));
    strcpy(novo->musica, musica);
    strcpy(novo->Artista, Artista);
    novo->duracao = duracao;
    novo->proximo = l;
    return novo;

}
void imprime(struct Play_list * l)

{
    struct Play_list * p;
    for(p = l; p != NULL; p = p->proximo)

    {
        printf("Musica : %s \n", p->musica);
        printf("ARTISTA : %s \n", p->Artista);
        printf("Duracao : %.2f \n", p->duracao);
}
}

int busca(struct Play_list * l, char * musica)
{
    struct Play_list * p;
    int i = 1;
    for (p = l; p != NULL; p = p->proximo)

    {
        if (strcmp(musica, p->musica) == 0)

        {

            printf("Musica: %s %s %s \n", p->musica);
            return i;

        }   i++;

    }  return 0;

}

    main()

    {
 char grupo[30], artista[30];
 float duracao;
 struct Play_list * l;
 char resp = 's';
 char procura[30];

 printf(" ************************ \n");
 printf("|    PLAYER              |\n");
 printf("|          DE            |\n");
 printf("|             MUSICAS    |\n");
 printf("|                        |\n");
 printf(" ************************ \n");
 printf("\n\n");


 l = cria();
 while (resp != 'n')
 {
     printf("Qual eh a Musica? \n");
     scanf("%[^\n]", grupo);
     fflush(stdin);
     printf("De quem eh a musica? \n");
     scanf("%[^\n]", artista);
     fflush(stdin);
     printf("Qual eh o duracao? \n");
     scanf("%f", &duracao);
     fflush(stdin);
     l = insere(l, grupo, artista, duracao);
     printf("Continua? s/n \n");
     scanf("%c", &resp);
     fflush(stdin);

 }

 imprime(l);
 printf("Qual musica quer ouvir? \n");
 scanf_s("%s", procura);
 printf("Esta musica esta na posicao %d da play list\n", busca(l, procura));
 system("pause");
 }

inserir a descrição da imagem aqui

as you can see, it is not running in the correct order and if I continue to put the data, it is stopped.

inserir a descrição da imagem aqui

Can anyone give me a strength? I can’t find the error.

  • I edited the title, because there is no "C/C++" language...what exists is the C language, and the C language++

  • would not be a possible duplicate, because my problem is not in the storage of the data but in the order in which it is leaving.

1 answer

0

You can use the command setbuf(stdin, NULL); to clean the buffs between scans, because I’ve seen a lot of people complaining about fflush() by not working properly, in my projects I always use the setbuf when I have the same kind of problem.

Browser other questions tagged

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