Error when printing values, values that are printed have nothing to do with those that were saved

0

It turns out that the gets functions do not return any kind of value.   And the getMedidasPC (); always returns the values -858993460-858993460 regardless of what is written

Clientes.h

void getMedidasPC();
void setLargPC(int x);
int getLargPC();
void setAltPC(int y);
int getAltPC();

private:
int largPC;
int altPC;

clients.cpp

void Cliente::getMedidasPC()
{
cout << largPC << "mm x" << altPC << "mm" << endl;
cout << largPC;
cout << altPC;
}
void Cliente::setLargPC(int x)
{
largPC = x;
}

int Cliente::getLargPC()
{
return largPC;
}

void Cliente::setAltPC(int y)
{
altPC = y;
}

int Cliente::getAltPC()
{
return altPC;
}´

Main.cpp

int main(){
int xPC;
int yPC;

cout << "\n Largura:" << endl;
cin >> xPC;
clienteObj.setLargPC(xPC);
cout << "\n Altura:" << endl;
cin >> yPC;
clienteObj.setAltPC(yPC);

clienteObj.getLargPC(); //FUNCAO NAO ESTA A FUNCIONAR(não imprime nada)
clienteObj.getAltPC(); // ""     ""   ""  "" ""

cout << xPC << "e" << yPC << endl; //aqui é impresso '400e700'

clienteObj.getMedidasPC();//aqui é impresso -858993460mm x-858993460mm

-858993460-858993460

return 0;
}
    
asked by anonymous 06.07.2018 / 17:03

3 answers

1

The problem is that you have to initialize int largPC and int altPC before assigning a value to them, you can do this in the class constructor, type Cliente::Cliente() : largPC(0), altPC(0) .

    
06.07.2018 / 19:03
0

The functions getLargPC and getAltPC return an integer, but that does not mean that this integer will be automatically printed

You need to do this to print the resultant returned by these functions:

cout << "Resultado de getLargPC: " << getLargPC() << endl;

cout << "Resultado de getAltPC: " << getAltPC() << endl;
    
07.07.2018 / 04:00
0

Your code has several syntax errors, and does not even compile.

Your Clientes class does not have a constructor in which the attributes of class altPC and largPC could be initialized properly, which would avoid those strange returned values.

Follow your rewritten code so that it compiles and works as expected.

Client.h :

#ifndef CLIENTE_H
#define CLIENTE_H

class Cliente
{
    public:

        Cliente();    // Construtor
        ~Cliente();   // Destrutor

        void exibirMedidas();

        void setLargura(int x);
        int getLargura();

        void setAltura(int y);
        int getAltura();

    private:

        int largura;
        int altura;
};

#endif

Client.cpp :

#include <iostream>
#include "Cliente.h"

Cliente::Cliente()
{
    largura = 0;
    altura = 0;
}

Cliente::~Cliente()
{
}

void Cliente::exibirMedidas()
{
    std::cout << largura << "mm x " << altura << "mm" << std::endl;
}

void Cliente::setLargura(int x)
{
    largura = x;
}

int Cliente::getLargura()
{
    return largura;
}

void Cliente::setAltura(int y)
{
    altura = y;
}

int Cliente::getAltura()
{
    return altura;
}

main.cpp :

#include <iostream>
#include "Cliente.h"

using namespace std;

int main( void )
{
    Cliente c;
    int x;
    int y;

    cout << "Entre com a Largura: ";
    cin >> x;

    cout << "Entre com a Altura: ";
    cin >> y;

    c.setLargura(x);
    c.setAltura(y);

    c.exibirMedidas();

    cout << "Largura: " << c.getLargura() << endl;
    cout << "Altura: " << c.getAltura() << endl;

    return 0;
}

Output:

Entre com a Largura: 100
Entre com a Altura: 200
100mm x 200mm
Largura: 100
Altura: 200
    
07.07.2018 / 13:51