4
I have a program in which I need to store an array of a music class that generates a list of the songs and their characteristics. The problem is that at the time of the printing method it generates a confusion of the characters, or mixing or omitting part of them. I’ve tried a lot of methods and nothing’s worked out. Could you help me?
This is the main.cpp
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "musica.h"
#include <algorithm>
#include <string>
using namespace std;
int main(int argc, char** argv) {
int TAMMP3=0;
musica mp3, *musica_op;
musica_op = (musica*) malloc (1 * sizeof(musica));
int choice = 1;
do {
mp3 = musica();
mp3.lerdados();
musica_op[TAMMP3] = mp3;
if(choice == 1) {
musica_op = (musica*) realloc(musica_op,(TAMMP3+2) * sizeof(musica));
TAMMP3++;
}
cout<<"Deseja adicionar uma musica?\nDigite 1 para sim e 0 para nao :";
cin>>choice;
} while (choice == 1);
for(int i = 0; i < TAMMP3; i++) {
musica_op[i].imprimir();
}
return 0;
}
#endif
This is the music. h
#ifndef MUSICA_H
#define MUSICA_H
class musica {
public:
musica ();
~musica ();
char *GetNome_musica();
void SetNome_musica(char * m_nome_musica);
float GetDuracao_musica();
void SetDuracao_musica(float m_duracao_musica);
char *GetAlbum_musica();
void SetAlbum_musica(char *m_album_musica);
float GetKbps_musica();
void SetKbps_musica(float m_kbps_musica);
void SetAno(int m_ano);
int GetAno();
char *GetArtista();
void SetArtista(char *m_artista);
char *GetGenero();
void SetGenero(char *m_genero);
void lerdados ();
void imprimir();
private:
char * nome_musica;
float duracao_musica;
char * album_musica;
float kbps_musica;
int ano;
char * artista;
char * genero;
};
#endif
And that’s the music.cpp
#include "musica.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <string>
musica::musica(){
nome_musica = (char*) malloc (70*sizeof (char));
album_musica = (char*) malloc (70*sizeof (char));
artista = (char*) malloc (70*sizeof (char));
genero = (char*) malloc (70*sizeof (char));
}
musica::~musica(){}
void musica::SetNome_musica(char * m_nome_musica) {
nome_musica = m_nome_musica ;
}
char * musica::GetNome_musica() {
return nome_musica ;
}
void musica::SetAlbum_musica(char * m_album_musica) {
album_musica = m_album_musica;
}
char * musica::GetAlbum_musica() {
return album_musica;
}
void musica::SetArtista(char * m_artista) {
artista = m_artista;
}
char * musica::GetArtista() {
return artista;
}
void musica::SetGenero(char * m_genero) {
genero = m_genero;
}
char * musica::GetGenero() {
return genero;
}
void musica::SetDuracao_musica(float m_duracao_musica) {
duracao_musica = m_duracao_musica;
}
float musica::GetDuracao_musica() {
return duracao_musica;
}
void musica::SetKbps_musica(float m_kbps_musica) {
kbps_musica = m_kbps_musica;
}
float musica::GetKbps_musica() {
return kbps_musica;
}
void musica::SetAno(int m_ano) {
ano = m_ano;
}
int musica::GetAno() {
return ano;
}
void musica::imprimir() {
printf ("Nome da musica:%s"".mp3"" \nDuracao da musica:%10.2f\n Album da musica:%s \n "
"Kbps da musica:%10.2f \n Ano da musica:%d \n Nome do artista:%s \n Nome do genero:%s \n"
,&nome_musica,duracao_musica,&album_musica,kbps_musica,ano,&artista,&genero);
}
void musica::lerdados() {
printf("Informe o nome da musica:\n");
scanf (" %[^\n]s",&nome_musica);
printf("Informe a duracao da musica:\n");
scanf (" %f",&duracao_musica);
printf("Informe o nome do album:\n");
scanf (" %[^\n]s",&album_musica);
printf("Informe o Kbps da musica:\n");
scanf (" %f",&kbps_musica);
printf("Informe o ano da musica:\n");
scanf (" %d",&ano);
printf("Informe o artista da musica:\n");
scanf (" %[^\n]s",&artista);
printf("Informe o genero da musica:\n");
scanf (" %[^\n]s",&genero);
}
I suggest that we reduce the question to a small and verifiable example, because as it has it is very extensive and makes it more difficult to give an answer. So if you want an answer and quick ask a direct and concrete question only with the code relevant to the problem. Regarding the code itself the
printf
insideimprimir
uses%s
but then passes an address of astring
, that’s not right :printf ("Nome da musica:%s....\n",&nome_musica, ...
whennome_musica
is the typechar*
, soon you should just passnome_musica
.– Isac
Hello. I didn’t understand his answer very well. But I will illustrate. He collects the data in vector of the object music. However at the time of printing the collected it shows omission of part of the collected. The name only takes 8 characters and was passed the limit of 70.
– Raí Aris
What I said is you’re not calling the
printf
correctly. Confirm placeholders and associated values. I don’t know if this is the only mistake because the code is too long and I haven’t seen it all, but at least this part needs to be corrected.– Isac
Dude, take a look at this assignment here: mp3 = music();
– HiHello
You have to set your builders right. And overload the assignment operator. The copy being made is copy by value, it is copying memory address. When it destroys the created object on the right side, it erases everything from its object on the left side of the assignment. It will print trash at best.
– HiHello