enum class
Completed
Define enum class Light { Red, Green, Yellow }; with a function next(Light) using a switch, and toString. Read a starting colour and a number of steps; print each state.
Input
Line 1: Red, Green or Yellow. Line 2: steps.
Output
One line per step.
Example
Input
Red 4
Output
Green Yellow Red Green
Editor
C++
Hints
enum class values are scoped: Light::Red.
They don't convert to int implicitly.
Solution explanation
Scoped enums avoid name clashes and accidental integer arithmetic.
Show solution
#include <iostream>
#include <string>
using namespace std;
enum class Light { Red, Green, Yellow };
Light next(Light l) {
switch (l) {
case Light::Red: return Light::Green;
case Light::Green: return Light::Yellow;
default: return Light::Red;
}
}
string toString(Light l) {
switch (l) {
case Light::Red: return "Red";
case Light::Green: return "Green";
default: return "Yellow";
}
}
int main() {
string start;
int steps;
cin >> start >> steps;
Light l = start == "Red" ? Light::Red : start == "Green" ? Light::Green : Light::Yellow;
for (int i = 0; i < steps; i++) {
l = next(l);
cout << toString(l) << "\n";
}
return 0;
}