Error while printing values, the values that are printed have nothing to do with the values that were saved

Asked

Viewed 45 times

0

It turns out that the gets functions do not return any kind of value. And getMedidasPC(); always returns 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;

cpp customers.

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;
}
  • Where was declared the clienteObj ? It does not appear anywhere in your code. Anyway I could not reproduce your problem. See your code running on Ideone

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

0

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

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

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

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

0

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

Your class Clientes does not have a constructor in which class attributes altPC and largPC could be properly initialized, which would prevent these strangers returned values.

Follow your code rewritten so that it 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;
}

Exit:

Entre com a Largura: 100
Entre com a Altura: 200
100mm x 200mm
Largura: 100
Altura: 200
  • I only copied a portion of the code here, so it’s normal to have misread something, thank you!

Browser other questions tagged

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