Changing brightness of a bulb

Essentially, in my program, I need to make a bulb change brightness according to user input. There are 3 states: off, dim, bright.

Should I have:
enum {off, dim, bright};
?
However, I am wondering say, the bulb is already bright, how do I change the brightness back to 0?

So:
1
2
3
4
5
6
7
8
9
10
11
12
13
enum {off, dim, bright};
int bulbs[5];
for (int i=0;i<5;i++)
{
bulbs[i]=bright;
}
bulbs [0] = bright;
for (int i=0; i<5;i++)
{
if (bulb[i]==bright) bulb[i]=off;
else if (bulb[i]==dim) bulb[i]=bright;
else if (bulb[i]==off) bulb[i]=dim;
}

Would that be a valid declaration?
Is there a more efficient method?
e.g. should I do bright++;

Thank you in advance!
Amy
x
Last edited on
How about this, it makes it easier to increment/decrement a bulb:

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
struct Bulb
{
	// default constructor - set bulb state to off:
	Bulb()
		: level(off)
	{
	}

	// custom constructor - set bulb state to argv[0]:
	Bulb(const short _level)
		: level(_level)
	{
	}

	enum Level
	{
		off,
		dim,
		bright
	};

	// increment instance : 0 to 1 to 2 to 0 ...
	short operator ++ ()
	{
		level++;
		if (level > bright)
			level = off;
		return level;
	}

	// decrement instance : 0 to 2 to 1 to 0 ...
	short operator -- ()
	{
		level--;
		if (level < off)
			level = bright;
		return level;
	}

	short level; 
};


int main()
{
	// declare an array of Bulb's:
	Bulb bulbs[] = {Bulb::bright, Bulb::bright, Bulb::bright, Bulb::bright, Bulb::bright};

	// we should have 5 Bulb's:
	const int COUNT = sizeof(bulbs)/sizeof(Bulb);

	// loop a number of times to change each state of a Bulb a few times:
	for (int n = 0; n < 9; ++n)
		for (int m = 0; m < COUNT; ++m)	// iterate through array
			bulbs[m]++;							// increment each bulb : 0 to 1 to 2 to 0 to 1 to 2 to 0 ...

	// return back to OS with no error:
	return 0;
}
Last edited on
Hiya, it looks rather complex!!!
I have only learnt a little bit about classes, so could you comment some parts of the code pls?
Also, in main(), if I chose to turn bulb 2 to dim, when it is off, how would I do that ?

Thank you very much for your help!
I have updated, code to add comments. HTH It is really quite simple, we have a struct that has a default constructor and a custom constructor. The struct has a single member variable 'level', and a ++ and -- operator so that each instance of a Bulb can be incremented/decremented. You previously had a if | else if | else construct to do the same as the ++ operator for a Bulb now does. Calling the increment operator ++ increments the Bulb::level through off | 0 to dim | 1 to bright | 2 then back through to off | 0 etc... this is what happens when you increment an integer through its entire range when you get to the upper bounds of an integer it will loop round ...

Does that help you understand?
Last edited on
Also, in main(), if I chose to turn bulb 2 to dim, when it is off, how would I do that ?


 
bulbs[1]++;

Last edited on
Great. I think I am starting to get the hang of it now.
so bulb 2 is bright and i want to dim it: bulbs[1]--
ajh32 overloaded the ++ and -- operators so that when you use them with an object of struct Bulb other operations are performed...

overloading is a way of adding operations to an operator...
Yes, that's correct.
Topic archived. No new replies allowed.