473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

My switch is not switching

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
--
Marian Aldenhövel, Rosenhain 23, 53123 Bonn
http://www.marian-aldenhoevel.de
"Success is the happy feeling you get between the time you
do something and the time you tell a woman what you did."
Aug 16 '08 #1
4 3056
Marian Aldenhövel wrote:
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:
ITYM case CS_WAIT_FOR_STX:

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

Incidentally, VC++ 2005 warns me when I do this:

warning C4065: switch statement contains 'default' but no 'case' labels
warning C4102: 'CS_WAIT_FOR_STX' : unreferenced label

Have you got all warnings turned on for GCC? It might have something
similar.

Regards,
Stu
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
Aug 16 '08 #2
On Aug 16, 8:46 am, Marian Aldenhövel <mar...@mba-software.dewrote:
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
--
Marian Aldenhövel, Rosenhain 23, 53123 Bonnhttp://www.marian-aldenhoevel.de
"Success is the happy feeling you get between the time you
do something and the time you tell a woman what you did."

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";
}
}
Aug 16 '08 #3
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 notused

Thank's again.

Ciao, MM
--
Marian Aldenhövel, Rosenhain 23, 53123 Bonn
http://www.marian-aldenhoevel.de
"Success is the happy feeling you get between the time you
do something and the time you tell a woman what you did."
Aug 16 '08 #4
Marian Aldenhövel wrote:
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.
Make it a habit to at least use -ansi -pedantic -Wall -Wextra

Aug 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
28
by: Ryan Liu | last post by:
Hi, I have a client/server application, using one thread/client approach. I see very high context switch/sec. What are the ways to reduce it? Each thread sleep longer in its endless loop if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.