Can i do this in an if statement?

Pages: 12
You need to review some of the basics of C++ again, here is a video explaining this:

http://thenewboston.org/watch.php?cat=16&number=11

i would recommend watching the entire series to refresh/learn
Ok i will. Lol his videos are the ones i watch :P. I didnt watch them all though i just skimmed through all them so i guess i need to watch them in order, luckily i have them all downloaded to my computer.
These videos should provide a very basic understanding of the language. After the tutorials i HIGHLY recommend buying books and reading them while trying to write/solve as many problems as possible inside the books.
Could you guys tell me once more what i need to work on in my program? What parts should i fax and or change, dont tell me how to do it though just tell me what needs to be done, that way i can try to figure it out and hopefully fix the problems and make my program work better. I already know about the class but what else was there?
Biggest things to learn are function return types, string manipulation (typically referred to as parsing), overall OOP (specifically class definitions and creating objects), and possibly just overall syntax with using the above within your code.
I have been watching tutorials and stuff is this better? it works perfectly

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>

using namespace std;

class Enemy
{
    protected:
        int attackPower;
    public:
        void setAttackPower(int a)
        {
            attackPower = a;
        }
};

class Stats
{
    protected:
        int experience;
        int health;
        int money;
        int pattack;
    public:
        void setExperience(int b)
        {
            experience = b;
        }
        void setHealth(int c)
        {
            health = c;
        }
        void setMoney(int d)
        {
            money = d;
        }
        void setAttack(int e)
        {
            pattack = e;
        }
};

class Trex: public Enemy
{
    public:
        void attack()
        {
            cout << "T-rex attacked! -" << attackPower << endl;
        }
};

class Raptor: public Enemy
{
    public:
        void attack()
        {
            cout << "Raptor attacked! -" << attackPower << endl;
        }
};

class Player: public Stats
{
    public:
        void attack()
        {
            cout << "you attacked! -" << pattack << endl;
        }
        void Money()
        {
            cout << "you earned money! +$" << money << endl;
        }
        void Experience()
        {
            cout << "you gained experience! +" << experience << endl;
        }
        void Health()
        {
            cout << "Your current health =" << health << endl;
        }
};

int main()
{
    Trex t;
    Raptor r;
    Enemy *enemy1 = &t;
    Enemy *enemy2 = &r;
    enemy1->setAttackPower(20);
    enemy2->setAttackPower(25);
    t.attack();
    r.attack();
    cout << "\n";
    Player p;
    Stats *EXP = &p;
    Stats *HLTH = &p;
    Stats *MNY = &p;
    Stats *ATCK = &p;
    EXP->setExperience(0);
    HLTH->setHealth(100);
    MNY->setMoney(0);
    ATCK->setAttack(25);
    p.attack();
    p.Money();
    p.Health();
    p.Experience();
}


Am i doing anything wrong or does it look ok?
Last edited on
bump
_ No encapsulation (getters and setters are evil http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html )
_ Inheritance abuse (¿Player is a Stats?)
_
1
2
    Enemy *enemy1 = &t;
    enemy1->setAttackPower(20);
Explain the purpose of 'enemy1'. (¿do you know Aceix?)
_ No const--correctness.
_ ¿relationships?
idk thats just the way the guy showed me how to do it. I watched this guys entire C++ series

http://thenewboston.org/watch.php?cat=16&number=11
Last edited on
bump
¿what do you want? you change nothing and didn't answer me.
ok well i have the same basic program and i want to be able to get the string from the user and write it to name in the class but im confused as to how to do that. He didnt show how to in the tutorial.

This doesnt give me errors but i want it to show the name in the other function which it doesnt

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>

using namespace std;

class Enemy
{
    protected:
        int attackPower;
    public:
        void setAttackPower(int a)
        {
            attackPower = a;
        }
};

class Stats
{
    protected:
        int experience;
        int health;
        int money;
        int pattack;
        string name;
    public:
        void setExperience(int b)
        {
            experience = b;
        }
        void setHealth(int c)
        {
            health = c;
        }
        void setMoney(int d)
        {
            money = d;
        }
        void setAttack(int e)
        {
            pattack = e;
        }
        void setName(string f)
        {
            name = f;
        }
};

class Trex: public Enemy
{
    public:
        void attack()
        {
            cout << "T-rex attacked! -" << attackPower << endl;
        }
};

class Player: public Stats
{
    public:
        void attack()
        {
            cout << "you attacked! -" << pattack << endl;
        }
        void Money()
        {
            cout << "you earned money! +$" << money << endl;
        }
        void Experience()
        {
            cout << "you gained experience! +" << experience << endl;
        }
        void Health()
        {
            cout << "Your current health =" << health << endl;
        }
        void Name()
        {
            cin >> name;
        }
};

void test();

int main()
{
    string name;
    Player p;
    Stats *Pstats = &p;

    cout << "What is your name" << endl;
    cin >> name;

    Pstats->setName(name);

    test();
}

void test()
{
    string name;
    Player p;
    Stats *Pstats = &p;

    Pstats->setName(name);

    cout << name << endl;
}
Because you aren't passing the object Pstats, you're creating a new one that has only default values.

Your code has the same effect as if I were to create a file called happy.txt and put some data in it, then I messaged you and said hey, create a file called happy.txt and tell me what the first line says. Since you just created it, there isnt going to be anything there.

A more beneficial solution would be for me to send you that file. You need to send your object to the function.
Ok how would that look again.
Pass by-value Syntax:
functionDataType FunctionName(param1DataType param1Name, ...)

You can also pass by reference as well. This would be similar to using a connection directly to my computer to allow you to read it and to modify it as well.

Pass by-reference Syntax:
functionDataType FunctionName(param1DataType &param1Name, ...)
i remember now. ok it works but i dont want to reference alot of stuff is there another way to do it in any of my classes? i want the name typed in to be saved in the string name in the class player. Also im trying to output money but it wont show it even though it compiles fine

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>

using namespace std;

class Enemy
{
    protected:
        int attackPower;
    public:
        void setAttackPower(int a)
        {
            attackPower = a;
        }
};

class Stats
{
    protected:
        int experience;
        int health;
        int money;
        int pattack;
        string name;
    public:
        void setExperience(int b)
        {
            experience = b;
        }
        void setHealth(int c)
        {
            health = c;
        }
        void setMoney(int d)
        {
            money = d;
        }
        void setAttack(int e)
        {
            pattack = e;
        }
        void setName(string f)
        {
            name = f;
        }
};

class Trex: public Enemy
{
    public:
        void attack()
        {
            cout << "T-rex attacked! -" << attackPower << endl;
        }
};

class Player: public Stats
{
    public:
        void attack()
        {
            cout << "you attacked! -" << pattack << endl;
        }
        int Money()
        {
            return (this) -> money;
        }
        void Experience()
        {
            cout << "you gained experience! +" << experience << endl;
        }
        void Health()
        {
            cout << "Your current health =" << health << endl;
        }
        void Name()
        {
            name;
        }
};

void test(string &name);

int main()
{
    string name;
    Player p;
    Stats *Pstats = &p;

    cout << "Enter your name" << endl;
    getline(cin, name);

    test(name);
}

void test(string &name)
{
    Player p;
    Stats *Pstats = &p;
    Pstats->setMoney(000);

    cout << "Welcome " << name << " to the game" << endl;
    cout << "your current stats are this: " << endl;
    p.Money();

}
Last edited on
bump
Ok so i was watching this video on polymorphism and it was way better than the others, i kind of understand stuff better but there are some things im confused on what they are:

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
#include <iostream>
#include <string>
using namespace std;

class Weapon
{
    private:
        int attack;
        string name;

    public:
        Weapon(const string & n, int a): name(n), attack(a)
        {

        }
        const string & get_name()
        {
            return name;
        }
        int get_attack()
        {
            return attack;
        }
};


class Sword: public Weapon
{
    private:
        int sharpness;
    public:
        Sword(const string & n, int a, int s): Weapon(n,a), sharpness(s)
        {

        }
};

class MagicAttack: public Weapon
{
    private:
        int MpCost;
    public:
        MagicAttack(const string & n, int a, int mpc): Weapon(n,a), MpCost(mpc)
        {}
};

int main()
{
    Weapon* wsword = new Sword("Red Sword", 15, 5);
    Weapon* wmagic = new MagicAttack("Lightning", 45, 200);

    Weapon* weapons[2] = {wsword, wmagic};

    for(int i = 0; i <2; i++)
    {
        cout << weapons[i]->get_name() << ": " << weapons[i]->get_attack() << endl;
    }

    delete wsword;
    delete wmagic;
}


What is const and new

why did he do this : name(n), attack(a) after weapon
and :public Weapon after the other classes, i think i might understand that, it so he can use the vars in Weapon in the child classes? also what is
string & n doing, why is he referencing n? i think i know why but tell me just in case.

he also said that you need to delete wsword and wsmagic but didnt really say why i dont think. Other than that i pretty much understand everything else. well there are a few more things but lets just start with this for now. I would google this stuff but i always get technical answers, and for me to understand it i need it explained in lamens terms, a very un technical explanation will help me understand this stuff. thanks in advanced.
Last edited on
closed account (o3hC5Di1)
I would advise you to create a new topic for these questions.
Apart from being the same project,, it has little relevance to your original questions.

All the best,
NwN
ok i'll do that.
Topic archived. No new replies allowed.
Pages: 12