PLEASE HELP

hey guys please help me :(
the code is not working
here's the question
Construct a class named Light that simulates a traffic light. The class color attribute should change from green to yellow to red and then back to green by using the class change () function. When a new light is created its initial color should be red Thanks for helping ! :)

code: i've just seen this code here please explain it to me please cause i can't understand thanks

#include <iostream>
#include <string>
using namespace std;

class Light
{
private:
string color;

public:
Light();
void chgColor();
};

void Light::chgColor()
{

}

int main()
{
int mRed, mYellow, mGreen;
Light light;
light.chgColor(); // To change color
return 0;
}
Light::Light() : mRed(true), mYellow(false), mGreen(false)
{
// We don't need to do anything else in here, so it's left blank
}
void Light::chgColor()
{
// See if the light is red
if (mGreen) {
// Set it to false
mGreen = false;
// And make it yellow
mYellow = true;
}
// We need else ifs or the light will be green each time
else if (mYellow) {
mYellow = false;
mRed = true;
}
// Assume it's red
else {
mRed = false;
mGreen = true;
}
}
string Light::getColor()
{
if (mGreen)
return "Green";
else if (mYellow)
return "Yellow";
else
return "Red";
}
posting in code tags

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

class Light
{
private:
string color;

public:
Light();
void chgColor();
};

void Light::chgColor()
{

}

int main()
{
int mRed, mYellow, mGreen;
Light light;
light.chgColor(); // To change color
return 0;
}
Light::Light() : mRed(true), mYellow(false), mGreen(false)
{
// We don't need to do anything else in here, so it's left blank
}
void Light::chgColor()
{
// See if the light is red
if (mGreen) {
// Set it to false
mGreen = false;
// And make it yellow
mYellow = true;
}
// We need else ifs or the light will be green each time
else if (mYellow) {
mYellow = false;
mRed = true;
}
// Assume it's red
else {
mRed = false;
mGreen = true;
}
}
string Light::getColor()
{
if (mGreen)
return "Green";
else if (mYellow)
return "Yellow";
else
return "Red";
} 

It looks better with indentations:
This compiles and runs with code blocks but has a lot of build warnings.

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

class Light
{
    private:
    string color;
    bool mRed,mGreen,mYellow;
    public:
    Light();
    void chgColor();
    string getColor();
};

Light::Light() : mRed(true), mYellow(false), mGreen(false)
{
    // We don't need to do anything else in here, so it's left blank
}

void Light::chgColor()
{
    // See if the light is red
    if (mGreen) {
        // Set it to false
        mGreen = false;
        // And make it yellow
        mYellow = true;
    }

    // We need else ifs or the light will be green each time
    else if (mYellow) {
        mYellow = false;
        mRed = true;
    }
    // Assume it's red
    else {
        mRed = false;
        mGreen = true;
    }
}

string Light::getColor()
{
    if (mGreen)
    return "Green";
    else if (mYellow)
    return "Yellow";
    else
    return "Red";
}

int main()
{
    int mRed, mYellow, mGreen;
    Light light;
    cout << light.getColor() << endl;
    light.chgColor(); // To change color
    cout << light.getColor() << endl;
    light.chgColor();
    cout << light.getColor() << endl;
    return 0;
}


Last edited on
It is late. Hopefully this helps it is my take your problem. Hopefully this helps with some of the points you are struggling with.

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
#include <iostream>
enum LightColor {RED, YELLOW, GREEN, OFF};

enum STATEOFLIGHT { STOP, SLOW , GO};

const char *stateOfLightNames[] = { "Stop", "Slow", "Go" };

const char *colorNames[] = { "Red", "Yellow", "Green", "Off" };

class Light {
    LightColor currColor;

public:
    Light()
    {
        this->currColor = OFF;

    }
    void setColor(LightColor color) {
        this->currColor = color;

    }
};

class TrafficLight {
private:
    Light lightColor;

    STATEOFLIGHT lightstate;

public:
    TrafficLight()
    {
        lightColor.setColor(OFF);

    }
    void SetStateOfLight(STATEOFLIGHT lightstate)
    {
        this->lightstate = lightstate;

        switch (lightstate) {
        case GO:
            lightColor.setColor(GREEN);

            break;

        case SLOW:
            lightColor.setColor(YELLOW);

            break;

        case STOP:
            lightColor.setColor(RED);

            break;

        }
    }
    STATEOFLIGHT getStateOfLight() {
        return this->lightstate;

    }
    void print() {
        std::cout << colorNames[getStateOfLight()] << std::endl;

    }
};

int main(int argc, const char * argv[]) {
    TrafficLight myLight;

    myLight.SetStateOfLight(GO);

    myLight.print();

    myLight.SetStateOfLight(STOP);

    myLight.print();

    myLight.SetStateOfLight(SLOW);

    myLight.print();

    return 0;

}


Green
Red
Yellow
Last edited on
Topic archived. No new replies allowed.