My switch is not switching

M

Marian Aldenhövel

Hi,

To handle a custom protocol over a serial line I am trying to implement
a very small and simple state machine. The states are declared as enum:

enum CommState {
CS_WAIT_FOR_STX,
CS_WAIT_FOR_T,
CS_WAIT_FOR_0,
CS_WAIT_FOR_Key,
CS_WAIT_FOR_ETX
};

I wanted to switch on the current state and handle whatever needs to be
done in that state plus the transition to the next one. However, my
switch() is always falling through to the default case.

I have cut down my code to this nonsensical example:

CommState commState=CS_WAIT_FOR_STX;

if (commState==CS_WAIT_FOR_STX) {
std::cout << "commState==CS_WAIT_FOR_STX" << std::endl;
}

switch (commState) {
CS_WAIT_FOR_STX:
std::cout << "switch-label CS_WAIT_FOR_STX" << std::endl;
break;
default:
std::cout << "default" << std::endl;
}

I would have expected this to output

commState==CS_WAIT_FOR_STX
switch-label CS_WAIT_FOR_STX

But I am getting:

commState==CS_WAIT_FOR_STX
default

What am I doing wrong?

The Compiler is GCC 4.2.3, on Ubuntu 7.

Ciao, MM
 
S

Salt_Peter

Hi,

To handle a custom protocol over a serial line I am trying to implement
a very small and simple state machine. The states are declared as enum:

enum CommState {
CS_WAIT_FOR_STX,
CS_WAIT_FOR_T,
CS_WAIT_FOR_0,
CS_WAIT_FOR_Key,
CS_WAIT_FOR_ETX
};

I wanted to switch on the current state and handle whatever needs to be
done in that state plus the transition to the next one. However, my
switch() is always falling through to the default case.

I have cut down my code to this nonsensical example:

CommState commState=CS_WAIT_FOR_STX;

if (commState==CS_WAIT_FOR_STX) {
std::cout << "commState==CS_WAIT_FOR_STX" << std::endl;
}

switch (commState) {
CS_WAIT_FOR_STX:
std::cout << "switch-label CS_WAIT_FOR_STX" << std::endl;
break;
default:
std::cout << "default" << std::endl;
}

I would have expected this to output

commState==CS_WAIT_FOR_STX
switch-label CS_WAIT_FOR_STX

But I am getting:

commState==CS_WAIT_FOR_STX
default

What am I doing wrong?

The Compiler is GCC 4.2.3, on Ubuntu 7.

Ciao, MM


Try it like this:

#include <iostream>

namespace state
{
enum Comm {
CS_WAIT_FOR_STX,
CS_WAIT_FOR_T,
CS_WAIT_FOR_0,
CS_WAIT_FOR_Key,
CS_WAIT_FOR_ETX };
}

int main()
{
const state::Comm test = state::CS_WAIT_FOR_STX;

switch( test )
{
case state::CS_WAIT_FOR_STX:
std::cout << "switch-label CS_WAIT_FOR_STX\n";
break;
default:
std::cout << "default state\n";
}
}
 
M

Marian Aldenhövel

Hi,
You've just defined a label there (oops!)

I KNEW it was going to be something really, really dumb. Thank you
very much!
Have you got all warnings turned on for GCC? It might have something
similar.

No, hadn't. Another stupid mistake. Now, compiling with -Wall, I get:

KeyboardHandler.cpp:205: warning: label 'CS_WAIT_FOR_STX' defined but not used

Thank's again.

Ciao, MM
 
R

Rolf Magnus

Marian said:
Hi,


I KNEW it was going to be something really, really dumb. Thank you
very much!


No, hadn't. Another stupid mistake. Now, compiling with -Wall, I get:

KeyboardHandler.cpp:205: warning: label 'CS_WAIT_FOR_STX' defined but not
used

Thank's again.

Make it a habit to at least use -ansi -pedantic -Wall -Wextra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top