I'm trying to program a simple state machine for an assignment. The assignment is to build a code that, given a string for input, can start at the βCatβ state, and perform actions until I run out of information.
Here's a chart which portrays what I'm trying to do:
Now, I've almost finished the code but there's a problem within a function. I am getting the error "invalid operands to binary expression ('State' and 'State')" Can someone give me a hint on how to correct this problem and a brief explanation on what's wrong? The problem is with using struct types in an if..else statement.
I have this portion of the code before int main():
struct State{
string A, B;
};
State Cat = {"Meow", "Ignore"};
State Noise = {"Boing", "Thud"};
State Food = {"Lemons", "Cinnamon"};
State mystate = Cat;
//my_state.A -> string
And here is the function where the error is:
void change_state(char c) {
// on taking character c, it changes current state
// If state is Cat and I get 1 , change to Food
// If state is Cat and I get 2 , change to Noise
// If state is Food and I get 1 , change to Noise
// If state is Food and I get 2 , change to Cat
// If state is Noise and I get 1 , change to Cat
// If state is Noise and I get 2 , change to Food
if (mystate == Cat){ //error
if (c == '1') {
mystate = Food;
}
else {
mystate = Noise;
}
}
else if (mystate == Food) {
if (c == '1') {
mystate = Noise;
}
else {
mystate = Cat;
}
}
else {
if (c == '1') {
mystate = Cat;
}
else {
mystate = Food;
}
}
}
Any help will be appreciated!