Variable not holding the assigned value

Asked

Viewed 81 times

1

I’m doing a project and I came across a problem.

#include "iostream"
#include <cstdlib>

using namespace std;

int per2 (void);
int per3 (void);
int per4 (void);

int main(void)
{
    per2();
    per3();
    per4();
}

per2()
{
int r2;
cout << "Coloque um número" << endl;
cin >> r2;
per3 ();
}

per3()
{
int r3;
cout << "Coloque outro número" << endl;
cin >> r3;
per4 ();
}

per4()
{
cout << r2 + r3
}

However, how r2 and r3 are of different Scope’s, when entering the per4 information is lost and I have to reset, is there any way I can determine the value of r2 and r3 be prevalent even outside Scope?

Edit: I know that in this simplified case, it would be simpler to do everything within the same Cope, but in my case I’m doing it in different, that’s the question.

2 answers

3


I’ll leave a well-commented code with a solution to your problem, but if you don’t know the object orientation paradigm I suggest you study a little to understand everything that’s going on. The code is well commented, but it is of no use if you do not know a little of the fundamentals of object orientation. Object orientation is widely used and is a good practice of programming, including to solve your problem, since instead of defining global variables or things like that, you define a class that will treat this problem.


When reading the code you will notice that the variables are private and have scope only within the class, but the functions are public. This means that the function can be called outside the class and access the values of the variables within it. But you cannot directly access the variables through the class object. This makes the code more organized and secure.


#include "iostream"
#include <cstdlib>

using namespace std;

class GuardaRespostas{ //criando uma classe

private: // criando variáveis privadas, ou seja, possuem o escopo somente dentro da classe
    int r1;
    int r2;
    int r3;

public: // funções públicas que podem ser acessadas fora do escopo da classe( inclusive dentro do main)

    GuardaRespostas(){ //Construtor da classe, é o método que será chamado automaticamente quando a classe for instanciada*
        r1=0;
        r2=0;
        r3=0;
    }

    //todas as funções da classe tem acesso às variáveis da classe

    void pergunta2(){  // colocar nomes significativos nas funções é uma boa prática para não se perder em códigos maiores
        cout << "Coloque um número" << endl;
        cin >> r2;
    }

    void pergunta3(){
        cout << "Coloque outro número" << endl;
        cin >> r3;
    }

    void pergunta4(){
        cout << "O resultado e: " << r2 + r3 << endl;
    }

};

int main()
{
    GuardaRespostas *guardaRespostas = new GuardaRespostas(); //Declarando e instanciando o objeto da classe
    guardaRespostas->pergunta2(); // Como usamos ponteiro para instanciar nosso objeto da classe
    guardaRespostas->pergunta3(); // Utilizamos setas "->" para acessar funções da classe
    guardaRespostas->pergunta4();

    system("pause");
    return 0;
}

  • It worked, thank you! Now I’ll give a study on this subject to know what I’m doing.

2

Darius, good afternoon!

As you said yourself, the scope of the variables are different, to persist this value, you would have to pass it as parameter in the functions you are using OR declare the variables in a global scope.

I hope I’ve helped with your doubt.

Example:

Dentro da função per2()
 ...
 per3(r2) //terminar assim

Na declaração da função per3
per3(int r2)
...
//terminar chamando a função per4 assim
per4(r2, r3)

e na função per4 começar assim
per4(int r2, int r3)
{...

Browser other questions tagged

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