Constructor Help

I need to add a constructor to this code, but I don't know how, even after looking it up. The code does everything it should, but apparently, it needs a constructor.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<vector>
#include<windows.h>

using namespace std;

class Buffalo{

    public:
        string name;
        string input;
        int hungerLevel = 5;
        int exhaustLevel = 5;
        int happiness = 0;
        void sayHi();
        void update();
        void roam();
        void sleep();
        void graze();
        void grunt();
        void choice();
        int x = 0;
};

void Buffalo::sayHi()
{
    name = "BUFF";
    cout << "Howdy, i'm " << name << " my hunger level is " << hungerLevel << " and my exhaustion level is " << exhaustLevel <<  ".\n";
}

void Buffalo::update()
{
    if(happiness >= 20){
        cout << endl;
        cout << endl;
        cout << name << " died today. Rest in peace my friend." << endl;
        Sleep(90000);
    }
    else if(happiness < 20){
        cout << "The buffalo lives and is still going strong." << endl;
        cout << endl;
    }
}

void Buffalo::roam()
{
    hungerLevel += 2;
    exhaustLevel += 2;
    cout << "The buffalo decided to roam around." << endl;
    cout << endl;
}

void Buffalo::sleep()
{
    hungerLevel += 2;
    exhaustLevel -= 3;
    cout << "The buffalo was tired so he went to bed." << endl;
    cout << endl;
}

void Buffalo::graze()
{
    hungerLevel -= 2;
    exhaustLevel += 2;
    cout << "The buffalo was hungry and went off to eat." << endl;
    cout << endl;
}

void Buffalo::grunt()
{
    happiness = hungerLevel + exhaustLevel;
    if(happiness <= 5 and happiness > 0)
    {
        cout << "grunt" << endl;
    }
    else if(happiness <= 10 and happiness > 5)
    {
        cout << "grrrrr..." << endl;
    }
    else if(happiness <= 15 and happiness > 10)
    {
        cout << "sniffle" << endl;
    }
    else if(happiness > 15)
    {
        cout << "wheeze" << endl;
    }
}

int main(){

    int loop = 1;
    Buffalo BUFF;
    string input;
    BUFF.x = 0;
    BUFF.sayHi();

    while(loop == 1){
        cout << " What would you like BUFF to do? 1: graze, 2: sleep 3: roam" << endl;
        cin >> input;
        if(input == "1"){
            BUFF.graze();
            BUFF.update();
            BUFF.grunt();
        }
        else if(input == "2"){
            BUFF.sleep();
            BUFF.update();
            BUFF.grunt();
        }
        else if(input == "3"){
            BUFF.roam();
            BUFF.update();
            BUFF.grunt();
        }

}
}
I see none of your Buffalo have a name. Sad times :(

Why not give them a name in the constructor, like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Buffalo{

    public:
        string name;
        string input;
        int hungerLevel = 5;
        int exhaustLevel = 5;
        int happiness = 0;
        void sayHi();
        void update();
        void roam();
        void sleep();
        void graze();
        void grunt();
        void choice();
        int x = 0;
        
    Buffalo(const string& inputName); // Declare a constructor
};



1
2
3
4
 Buffalo::Buffalo(const string& inputname) // define the constructor
  {
    name = inputname;
  }
Last edited on
You also never use your Buffalo.input member variable, I would consider deleting it if this is an assignment.

Edit: And on that same thought, what is the point of your buffalo.x variable?
Last edited on
srry ive been in other classes. Thats a great idea, repeater, I meant to use those but never got around to it. Ganado I need to delete that, it's extra stuff that I don't need :)
Topic archived. No new replies allowed.