Read the elements of a struct in another function

Asked

Viewed 490 times

-1

I need to access data from struct in my job CalcN, how should I proceed?

    int main(){

    int calcn();

    struct user1{

    string Nome;
    int Dnx;
    int Nxhu;

};

struct user1 x;

x.Dnx = 4;
x.Nxhu = 3;

int ResultadoEZ = calcn();

cout << "Resultado =  " << ResultadoEZ << endl;

}

    int calcn(){

        int Resultado;

        srand ( time (0) ) ;

        //Resultado = // x.Dnx + (rand() % 4); Dados da Struct 

        return Resultado;

    }
  • The ideal is to always post like this: http://answall.com/help/mcve. It makes it easier for us to test and find the problem. You don’t know how to address parameters in functions?

  • Just one minute, I’ll correct

  • I program in Java, I started with c++ yesterday. xD

  • It did not help much, the code that is there does not compile and is different from the previous one that I had already started to answer.

  • ready, already corrected, I will make some changes to be of better understanding

  • I think now I understand more or less right :P , is that I am programming a role-playing game, and I need to take the parameters of a character of the player who is in my main and move to another function. .

  • To be quite honest, if you were having difficulty in a language you know, you will get even more in a language you do not know and is one of the most difficult to master.

  • The game runs normally in Java, I just go to c++ because of the compiler :P , and I need the c++ for some other things also that Java does not allow me. I know the difficulties of c/c++, I have free time for studies, I’m enjoying it so far.

Show 3 more comments

1 answer

1


AP changed the question and put another example but the basis of what is done here serves for the same thing. You need to pass the object with this structure through function parameter.

Read how functions work in C++.

Then you need to put a parameter to the function to receive this data.

int CalcJnxU(User1 x) {
    int Resultado;
    srand(time(0));
    Resultado = x.hNX2 + (rand() % 4);
    return Resultado;
}

And then you’ll call the function by passing the die as argument.

User1 x;
x.Nome = "nome1"; //talvez essa variável nome1 exista em outro contexto, aí poderia mantê-la
x.NxZ = 20;
x.hNX2 = 2;
x.kJXU = 2;
CalcJnxU(x);

Note that the x the function is with the same name but would not need to be. You could put the variable name you want in the parameter. So you’re passing the amount you’re in x in the Main() to the x of function CalcJnxU() which are independent variables with different memory allocations. The data will be copied from one variable to the other (you can avoid this copy in this case, but let’s not complicate).

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Notice how my code always gets a lot easier to read regardless of what it does.

Then you will have to learn more advanced things. The way it is coded there may be small inefficiencies. Of course you don’t need to and can’t worry about it while you’re learning the basics. It will also be useful to learn typedef but leave it for later.

Browser other questions tagged

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