A newbie question

Hello guys, so, i started few days ago and i'm having some trouble with my code that im using to learn, i put in main to express a value (string) but it comes blank. Please help im really lost.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

class Talking{
    public:

        void setName(string x){
            name = x;
        }
        string getName(){
        return name;
        }
    private:
        string name;

};

class totprocess
{
    public:
        void setValue(int x)
        {
            z = x;
        }
        unsigned int getValue(){
        return z;
        }

    private:
    unsigned int z;
};

int tot()
{
    unsigned int x = 0;
    unsigned int y = 0;
    unsigned int z;
    srand(unsigned(time(0)));
    totprocess tot1;

    x = rand()%2;
    y = rand()%2;
    if(x==y)
    {
        tot1.setValue(1) ;
    }
    if(x>y)
    {
        tot1.setValue(0) ;
    }
    if(x<y)
    {
        tot1.setValue(0) ;
    }
    if(z==y==0)
    {
        tot1.setValue(0);
    }
    if(x==0)
    {
        y=0;
    }

}
int totpostprocessing()
{
    unsigned int z;
    totprocess tot2;
    Talking to;
    z = tot2.getValue();
    if(z==1)
    {
        to.setName("Yes");
    }
    if(z==0)
    {
        to.setName("No");
    }
}

int main()
{
    Talking to;
    string name;
    name = to.getName();
    cout << name;
    return 0;
}
Last edited on
What value were you expecting to be displayed?

Line 87: You instantiate an instance of Talking named to. Talking's default constructor (provided by the compiler) is called. name at line 18 is instantiated using string's default constructor (an empty string).

Line 87: You call Talking's getter for name. name has never been modified, so what you're going to get is the empty string created when to was instantiated.

If you want to::name to be something other than an empty string, you need to call your setter for name.

1
2
// After line 87
  to.setName ("Fred");


First i want set the value of Z at function at line 37, than its writed in class at line 21, than function at line 69 get value of Z and write the string in class at line 8. In main i want to get what has been writen in class Talking., it should be "yes" or "no". Sorry for bad english and thankyou for you patience.
You're never going to reach line 37, because you never call tot.

Line 73: This is a separate instance of to. It is unrelated to line 87. It goes out of scope at line 83 and the value you set is lost. In any case, this is all irrelevant because you also never call totpostprocessing.


Topic archived. No new replies allowed.