Problems with scanf and printf in C++

Asked

Viewed 175 times

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);
}

  • 1

    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 inside imprimir uses %s but then passes an address of a string, that’s not right : printf ("Nome da musica:%s....\n",&nome_musica, ... when nome_musica is the type char*, soon you should just pass nome_musica.

  • 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.

  • 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.

  • Dude, take a look at this assignment here: mp3 = music();

  • 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.

1 answer

1

In C++, try to avoid the use of functions printf() and scanf()! They can be perfectly replaced by std::cout and std::cin, respectively.

The same happens with strings, instead of using a pointer char* pointing to a memory area allocated with malloc(), you can just use std::string.

His method imprimir() can be replaced by the operator <<.

Just look how simple and readable everything can be:

#include <iostream>
#include <string>

class Musica
{
    public:

        Musica( void ) : m_duracao(0.0), m_kbps(0.0), m_ano(0) {}
        virtual ~Musica(void) {}

        void nome(std::string nome) { m_nome = nome; }
        std::string nome(void) { return m_nome; }

        void album( std::string album ) { m_album = album; }
        std::string album(void) { return m_album; }

        void artista(std::string artista) { m_artista = artista; }
        std::string artista(void) { return m_artista; }

        void genero(std::string genero) { m_genero = genero; }
        std::string genero(void) { return m_genero; }

        void duracao(float duracao) { m_duracao = duracao; }
        float duracao(void) { return m_duracao; }

        void kbps(float kbps) { m_kbps = kbps; }
        float kbps(void) { return m_kbps; };

        void ano(int ano) { m_ano = ano; }
        int ano(void) { return m_ano; }

        void lerdados( void )
        {
            std::cout << "Informe o nome da musica:" << std::endl;
            std::getline(std::cin, m_nome);

            std::cout << "Informe a duracao da musica:" << std::endl;
            std::cin >> m_duracao;

            std::cout << "Informe o nome do album:" << std::endl;
            std::cin.ignore();
            std::getline(std::cin, m_album);

            std::cout << "Informe o Kbps da musica:" << std::endl;
            std::cin >> m_kbps;

            std::cout << "Informe o ano da musica:" << std::endl;
            std::cin >> m_ano;

            std::cout << "Informe o artista da musica:" << std::endl;
            std::cin.ignore();
            std::getline(std::cin, m_artista);

            std::cout << "Informe o genero da musica:" << std::endl;
            std::getline(std::cin, m_genero);

            std::cout << std::endl;
        }

        friend std::ostream &operator<<( std::ostream &out, Musica obj )
        {
            out << "Musica:" << std::endl;
            out << "    Nome: " << obj.m_nome << std::endl;
            out << "    Duracao: " << obj.m_duracao << " segs" << std::endl;
            out << "    Album: " << obj.m_album << std::endl;
            out << "    Kbps: " <<  obj.m_kbps << " Kbps" << std::endl;
            out << "    Ano: " << obj.m_ano << std::endl;
            out << "    Artista: " << obj.m_artista << std::endl;
            out << "    Nome do genero: " << obj.m_genero << std::endl;

            return out;
        }

    private:

        std::string m_nome;
        float m_duracao;
        std::string m_album;
        float m_kbps;
        int m_ano;
        std::string m_artista;
        std::string m_genero;
};


int main( void )
{
    Musica m;
    m.lerdados();
    std::cout << m;
    return 0;
}

Exit:

$ ./musica 
Informe o nome da musica:
Time
Informe a duracao da musica:
408
Informe o nome do album:
The Dark Side of The Moon
Informe o Kbps da musica:
256
Informe o ano da musica:
1973
Informe o artista da musica:
Pink Floyd
Informe o genero da musica:
Rock

Musica:
    Nome: Time
    Duracao: 408 segs
    Album: The Dark Side of The Moon
    Kbps: 256 Kbps
    Ano: 1973
    Artista: Pink Floyd
    Nome do genero: Rock

Browser other questions tagged

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