### 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:
 ``12345678910111213`` ``````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++;

Amy
x
Last edited on

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````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 ...

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...