0

I'm creating a simple cine manager with C++ and, when I call a function to sell tickets, everything goes right: the method checks if there is money to buy, the tickets are printed and the new amount of money too. But if I print the money after call the function, my money resets to the money that I had when I created my object. What am I doing wrong?

#include <iostream>
#include <string.h>
using namespace std;

class Sala {
    public: 
        int id;
    public:
        Sala();
        Sala(int comprimento, int largura, int id);
        int comprimento;
        int largura;
        int getCapacidade();
        bool reservado;
};

class Lista{
    public: 
        Lista();
        Sala salas[50];
        int tamanho;
};


Lista:: Lista(){
    tamanho = 0;
};


Sala:: Sala(){
    reservado = false;
};

Sala:: Sala(int comprimento, int largura, int id){
    this->comprimento = comprimento;
    this->largura = largura;
    this->id = id;
    this->reservado = false;
};

int Sala:: getCapacidade(){
    return comprimento * largura;
}

class Sessao {
    public:
        Sala sala;
        string filme;
        int capacidade;
        int ingressos;
        float preco;
        Sessao(float preco, string filme, Sala sala);
        void comecarFilme();
};

Sessao:: Sessao(float preco, string filme, Sala sala) {
    this->preco = preco;
    this->filme = filme;
    this->sala = sala;
    this->capacidade = sala.getCapacidade();
    this->ingressos = 0;
};


class Manager {
    private:
        string nome;
        int idManager;
    public: 
        Manager(string nome, int idManager);
        void cadastrarSala(Lista lista, Sala sala);
        Sessao criarSessao(float preco, string filme, Sala sala);
        Sala reservarSala(Lista lista, Sessao sessao);
};

Manager:: Manager(string nome, int idManager){
    this->nome = nome;
    this->idManager = idManager;
}

void Manager:: cadastrarSala(Lista lista, Sala sala){
    lista.salas[lista.tamanho] = sala;
    lista.tamanho++;
}

Sessao Manager:: criarSessao(float preco, string filme, Sala sala){
    Sessao sessao(preco, filme, sala);
    return sessao;
}

Sala Manager:: reservarSala(Lista lista, Sessao sessao){
    int i = 0;
    
    while(lista.salas[i].reservado == true){
        i++;
    }
    lista.salas[i].reservado = true;
    sessao.sala = lista.salas[i];
    return lista.salas[i];
}

class Cliente{
    public: 
        string name;
        int idCliente;
        float dinheiro;
        int ingressosPossuidos;
        Cliente(string name, int id, float dinheiro);
        int compraIngresso(Sessao sessao, int quantidade);
};

Cliente:: Cliente(string name, int id, float dinheiro){
    this->name = name;
    this->idCliente = id;
    this->dinheiro = dinheiro;
    this->ingressosPossuidos = 0;
}

int Cliente:: compraIngresso(Sessao sessao, int quantidade){
    dinheiro -= quantidade * sessao.preco;
    ingressosPossuidos += quantidade;
    cout<<name<<" agora possui "<<dinheiro<<" reais"<<endl;
    cout<<name<<" agora possui "<<ingressosPossuidos<<" ingressos"<<endl;
    return ingressosPossuidos;
}

class Vendedor {
    private:
        string name;
        int idVendedor;
    public: 
        Vendedor(string nome, int idVendedor);
        void venderIngresso(Sessao sessao, int quantidade, Cliente cliente);
};

Vendedor::Vendedor(string nome, int idVendedor){
    this->name = nome;
    this->idVendedor = idVendedor;
}


void Vendedor::venderIngresso(Sessao sessao, int quantidade, Cliente cliente){
    if(quantidade > (sessao.capacidade - sessao.ingressos)){
        cout<<"Infelizmente o senhor "<<cliente.name<<" não conseguiu comprar os ingressos pois a sessão estava lotada!"<<endl;
    } else if ((quantidade * sessao.preco) > cliente.dinheiro){
        cout<<"Infelizmente o senhor "<<cliente.name<<" não conseguiu comprar os ingressos pois não tinha dinheiro suficiente!"<<endl;
    } else {
        cout<<"O senhor "<<cliente.name<<" comprou "<<quantidade<<" ingressos para a sessão "<<sessao.filme<<"!"<<endl;
        sessao.ingressos += quantidade;
        cliente.compraIngresso(sessao, quantidade);
    }
}



int main(){
    Manager manager("Hugo", 1);
    Lista listaSalas;

    Sala sala1(5, 8, 1);
    Sala sala2(10, 15, 2);

    manager.cadastrarSala(listaSalas, sala1);
    manager.cadastrarSala(listaSalas, sala2);


    Sessao sessao1(10, "Avengers", sala1);
    Sessao sessao2(20, "Star Wars", sala2);

    Vendedor vendedor1("Joao", 1);
    Vendedor vendedor2("Maria", 2);

    Cliente cliente1("Lucas", 1, 50);
    Cliente luiza("luiza", 2, 20);

    manager.reservarSala(listaSalas, sessao1);

    vendedor1.venderIngresso(sessao1, 5, cliente1);
    vendedor1.venderIngresso(sessao1, 10, cliente1);

    cout<<"O cliente "<<cliente1.name<<" tem "<<cliente1.dinheiro<<" reais!"<<endl;
    cout<<"O cliente "<<cliente1.name<<" tem "<<cliente1.ingressosPossuidos<<" ingressos!"<<endl;
    cout<<"A sessao "<<sessao1.filme<<" tem "<<sessao1.ingressos<<" ingressos!"<<endl;
};

The outputs are like this

Lucas tem 50 reais
O senhor Lucas comprou 5 ingressos para a sessão Avengers!
Lucas agora possui 0 reais
Lucas agora possui 5 ingressos
Lucas tem 50 reais
Infelizmente o senhor Lucas não conseguiu comprar os ingressos pois não tinha dinheiro suficiente!
O cliente Lucas tem 50 reais!
O cliente Lucas tem 0 ingressos!
A sessao Avengers tem 0 ingressos!

First, it show that Lucas has 5 tickets and 0 BRL, but after this, Lucas returns to have 0 tickets and 50 BRL, and the session has again 0 sold tickets.

Hugo Folloni
  • 317
  • 1
  • 2
  • 10
  • And learn about the member initialization list: https://en.cppreference.com/w/cpp/language/constructor and `std::vector`. – Goswin von Brederlow Jun 03 '22 at 02:34
  • I don't think so, because I don't want to pass a value by reference, I just want to change a variable of my Object inside other function. – Hugo Folloni Jun 03 '22 at 02:35
  • 1
    @HugoFolloni If you want to do that, you need to pass by reference – NathanOliver Jun 03 '22 at 02:36
  • 1
    In order to change an object that is passed to a function, using a reference is the best approach. Also, you have substantially more code in this question than necessary to demonstrate the problem. Please make a [mre] when posting questions in the future. – cigien Jun 03 '22 at 02:36
  • @HugoFolloni *I just want to change a variable of my Object inside other function* -- LOL -- That's *exactly* how you're supposed to change a value inside a function, and have those changes seen by the caller. You pass the value by reference. It's like saying "I don't want to drive the car by starting the engine". – PaulMcKenzie Jun 03 '22 at 03:10

0 Answers0