traffic light

Im Getting a but load of errors I have to make a traffic light. Im thinking I have things mixed around or something. any help is appreciated. I know basically I have to draw each rectangle and make it sleep for a duration of time.






#include <iostream>
#include<Windows.h>
#include <ctime>

using namespace std;


class TrafficLight
{
public:
enum LightState { RED = 0, YELLOW, GREEN };
const byte GREEN = 0xA0;
const byte YELLOW = 0xE0;
const byte RED = 0xC0;
const byte GRAY = 0x80;
const byte BLACK = 0x00;


public:
TrafficLight();
void Advance();
void Wait();
char* CurrentColor();
void drawRect(int x, int y, int width, int height, byte color);


private:
LightState color;
int wait[3];
};

void TrafficLight::drawRect(int x, int y, int width, int height, byte color)
{
COORD c = { x, y };
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
SetConsoleCursorPosition(handle, c);
cout << ' ';
c.X++;
}

c.X = x;
c.Y++;
}
}
TrafficLight::TrafficLight()
{
color RED;
wait[RED] = 10 * 1000;
wait[YELLOW] = 4 * 1000;
wait[GREEN] = 15 * 1000;
}

void TrafficLight::Advance()
{
switch (color)
{
case RED: color = GREEN; break;
case YELLOW: color = RED; break;
case GREEN: color = YELLOW; break;
}
};

void TrafficLight::Wait()
{
//Sleep
Sleep(1000);

}

char* TrafficLight::CurrentColor()
{
switch (color)
{
case RED: return "Red"; break;
case YELLOW: return "Yellow"; break;
case GREEN: return "Green"; break;
}

return "Error";
}

int main()
{
cout << TrafficLight::drawRect(int x, int y, int 14, int , byte color) <<
drawRect(int x, int y, int width, int height, byte color) <<
drawRect(int x, int y, int width, int height, byte color) <<
drawRect(int x, int y, int width, int height, byte color);
}

At first look:

1
2
3
4
5
6
enum LightState { RED = 0, YELLOW, GREEN };
const byte GREEN = 0xA0;
const byte YELLOW = 0xE0;
const byte RED = 0xC0;
const byte GRAY = 0x80;
const byte BLACK = 0x00;

You cannot put initialization in there.
 
enum LightState { RED = 0xA0, YELLOW = 0xE0, GREEN = 0xC0, GRAY = 0x80, BLACK = 0x00 };

Better like this.

1
2
3
4
5
6
7
TrafficLight::TrafficLight()
{
color RED; // Here you missed = character for assignment
wait[RED] = 10 * 1000;
wait[YELLOW] = 4 * 1000;
wait[GREEN] = 15 * 1000;
}


1
2
3
4
5
6
7
8
void TrafficLight::Advance()
{
switch (color)
{
case RED: color = GREEN; break; //It has to be LightState::GREEN
case YELLOW: color = RED; break; //It has to be LightState::RED
case GREEN: color = YELLOW; break; //It has to be LightState::YELLOW
}


1
2
3
4
5
6
7
int main()
{
cout << TrafficLight::drawRect(int x, int y, int 14, int , byte color) <<
drawRect(int x, int y, int width, int height, byte color) <<
drawRect(int x, int y, int width, int height, byte color) <<
drawRect(int x, int y, int width, int height, byte color);
}


DrawRect is not a static function so you need to instantiate an object of TrafficLight type to use it (or make it static. Fixed that, drawRect return void so you cannot giving the result type of drawRect as input of the cout stream. Then the function calling doesn't have any sense, it's like a function sign declaration.

1
2
3
4
5
6
7
int main()
{
	TrafficLight::drawRect(10, 10, 14, 14, TrafficLight::LightState::GREEN);
	TrafficLight::drawRect(10, 20, 14, 14, TrafficLight::LightState::RED);
	TrafficLight::drawRect(10, 30, 14, 14, TrafficLight::LightState::YELLOW);
	system("pause");
}


If you make the drawRect static, include <cstdlib> and change the main like this, the 3 colored rectangle will be printed out.

or you can instantiate an object
1
2
3
4
5
6
7
8
9
int main()
{
	TrafficLight myTrafficLight;
	myTrafficLight.drawRect(10, 10, 14, 14, TrafficLight::LightState::GREEN);
	myTrafficLight.drawRect(10, 20, 14, 14, TrafficLight::LightState::RED);
	myTrafficLight.drawRect(10, 30, 14, 14, TrafficLight::LightState::YELLOW);
	std::cout << std::endl;
	system("pause");
}
Last edited on
Ok, so youre saying to put initialize them all with in the enum LightState statement instead of underneath like I have them. also should I go ahead and take the drawRect out of my class, or can I code the integers in the class and call them properly as you stated above
I see the draw rect as a private function of the TrafficLight class; in my vision the class expose a function that take a color as input and hide all the drawing stuff to the user.
Topic archived. No new replies allowed.