Call a function based on the struct passed in the reference?

Asked

Viewed 302 times

0

How Do I call the menu function by passing more than one struct per reference according to the validation in the validation function ?

sorry I’m starting now and decided to do this little exercise to practice functions, classes, and structs and I came across this problem that I can’t solve. Can someone please help me ?

class Conta {

 private:


struct cliente_1 {
    string nome = "Odair";
    int conta = 739146;
    double senha = 759247;
    float saldo = 00.00;
    double cpf = 48730701875;
};

struct cliente_2 {
    string nome = "Leo";
    int conta = 135478;
    double senha = 789878;
    float saldo = 00.00;
    double cpf = 48730701875;
};


struct cliente_3 {
    string nome = "Caio";
    int conta = 789757;
    double senha = 24860165;
    float saldo = 00.00;
    double cpf = 48730701875;
};

struct clientes {

    cliente_1 c1;
    cliente_2 c2;
    cliente_3 c3;

};
 public:

void menu( cliente_1 cont) {


    cout << cont.nome << endl;
    system("pause");




}
void validacao(int x, double  y) {

    cliente_1 c1;
    cliente_1 c2;
    cliente_1 c3;


    if (x == 739396 || y == 759247) {

        menu(c1);
    }
    else if (x == 135478 || y == 789878) {

        menu(c2);
    }
    else if (x == 789757 || y == 24860165) {

        menu(c3);
    }



}

1 answer

1

Despite his 3 struct have exactly the same format, they are declared to be different so you will not be able to pass them to their function, the compiler will accuse as error.

You can create a single Struct and then create 3 instance of it.

struct Cliente {
    string nome;
    int conta;
    double senha;
    float saldo;
    double cpf;
};

Cliente c1, c2, c3;

// adiciona os valores aos Structs antes de começar a modificalos
void IniciarClientes()
{
    c1.nome = "Odair";
    c1.conta = 739146;
    c1.senha = 759247;
    c1.saldo = 00.00;
    c1.cpf = 48730701875;

    c2.nome = "Leo";
    // e continua o restante
}

// passando como referencia como você pediu na pergunta
void menu(Cliente *cliente) 
{
    cout << cliente->nome << endl;
    system("pause");
}

void validacao(int x, double  y)
{
    if (x == 739396 || y == 759247) 
    {
        // passando como referencia
        menu(&c1);
    }
    else if (x == 135478 || y == 789878) 
    {
        menu(&c2);
    }
    else if (x == 789757 || y == 24860165)
    {
        menu(&c3);
    }
}
  • Thank you I’ll try ...

  • can give me a little help, I did the way you taught me but, something on Cout does not display anything on the screen, it’s like skipping Cout and went straight to system pause.

  • if you add anything else into cout along with cliente->nome display?

  • No, but when I put it to display another double or float data instead of the name it appears a number half nothing to see type - 946856x84

  • you initialized the struct with the IniciarClientes()

  • Yes and called her both in the main int and in the valifacao function

  • I did a quick test here, apparently the problem is string within the struct, mute to char*

Show 2 more comments

Browser other questions tagged

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