I am trying to implement structures in C in a nested way to use it as if it were a Stack, I am having problems, I am trying to implement a solution to the following statement and the error it throws me
Firstly the structure that I am using and followed by this is the Push Method that I use to add a new singer to the Stack.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define ARRSIZE 10
int contador = 0;
struct Disco{
char titulo[30];
int numCanciones;
};
typedef struct Disco discos;
//Esstructura Cantante
struct Cantante {
int codigo;
char nombre[30];
int edad;
int numDiscos;
struct Disco elementos[10];
};
typedef struct Cantante cantante;
//Estructura de la Pila
struct Stack{
int top;
struct Cantante* elemento[ARRSIZE];
};
typedef struct Stack st;
//Inicializar
void inicializarPila(st *s) {
s->top = -1;
}
//Compruebo si esta vacia
int vacia(st *s){
if (s->top == -1)
return 1;
else
return 0;
}
//Compruebo si esta llena
int llena(st *s){
if (s->top == ARRSIZE - 1)
return 1;
else
return 0;
}
//Agregar elemento a la pila
void push(st *s, cantante* x){
if (llena(s)) {
printf("Pila llena");
} else {
s->top++;
s->elemento[s->top] = x;
printf("\nAgregado!");
}
contador++;
}
The problem is that when wanting to add a singer and when choosing the number of Discs always at the end of it, the console pauses and automatically ends the program and that when printing the Stack the Singers are repeated. Why is this? I teach my main.
int main(int argc, char **argv)
{
st *s = (st *)malloc(sizeof(st));
inicializarPila(s);
cantante *c = (cantante *)malloc(sizeof(cantante));
struct Disco* elementos[10];
int cod = 0;
system("CLS");
c->codigo = contador;
printf("Ingrese el nombre del Cantante: ");
scanf("%s",c->nombre);
printf("Ingrese la edad del Cantante: ");
scanf("%d",&c->edad);
printf("Ingrese el numero de discos: ");
scanf("%d",&c->numDiscos);
for(int i= 0; i<c->numDiscos; i++){
printf("Ingrese el titulo del disco: ");
scanf("%s",elementos[i]->titulo);
printf("\nIngrese el numero de canciones del disco: ");
scanf("%d",&elementos[i]->numCanciones);
}
for(int i= 0; i<c->numDiscos; i++){
strcpy(c->elementos[i].titulo, elementos[i]->titulo);
c->elementos[i].numCanciones = elementos[i]->numCanciones;
}
system("CLS");
push(s,c);
}
When entering 2 singers with different information:
My Stack print method:
void imprimirPila(st *s) {
printf("CONTADOR : %d\n\n",contador);
int aux = 0;
printf("Pila: ");
for (int i = 0; i < contador; i++) {
printf("---------------------------------------");
printf("\nCantante");
printf("\nCodigo: %d ", s->elemento[i]->codigo);
printf("\nNombre: %s ", s->elemento[i]->nombre);
printf("\nEdad: %d ", s->elemento[i]->edad);
printf("\nNumero de discos: %d \n", s->elemento[i]->numDiscos);
printf("\n\nInformacion de los discos\n\n");
for(int j= 0; j< s->elemento[i]->numDiscos; j++){
printf("\t[CD] #%d Titulo: %s",aux,s->elemento[i]-
>elementos[j].titulo);
printf("\n\t[CD] Numero de canciones: %d",s->elemento[i]-
>elementos[j].numCanciones);
aux++;
}
printf("\n");
printf("\n");
printf("---------------------------------------");
}
}
When indicating that there are more than 2 discs:
I've been trying to do my exercise for a while, could you tell me what I'm doing wrong, I'd appreciate it.