Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 31st, 2004, 06:57 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lookup (Dispatch) Table

Hi all:

I am still learning c++, so I will try to describe my problem and what I am try to do the best I can.

The basis of my program is to take data from the Serial I/O Port into a character array and analyze it and do all kinds of stuff with it. Right now what I am trying to do is take a byte (I am working with whole numbers, so my byte would = 2 characters) and compare it against a table of possible matches and if the byte matches, go to the function that corresponds with that byte.

I'll give an example here:

Lets say the whole number integer is 48. Now I want to take 48 and compare it against a list of commands (00 to 99) and if one matches go to the routine of that matching number. so when it compared 48 against 48 in the table it would then call the cmd48() function and go from there.

I hope I have been as clear as possible, so that you can understand what it is I am trying to do.

NOTE: I dont think a switch-Break statement would work since it only checks 1 character and obviously I am not working with only 1 character, so I dont know anything else to use.

Thanks

Reply With Quote
  #2 (permalink)  
Old October 31st, 2004, 07:51 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you have two ascii characters, say c1 and c2, just convert to an int and do the switch:

Code:
#include <cctype>

int main()
{

char c1, c2;
  int invalue;

  // stuff here to get the chars

  if (!std::isdigit(c1) || !std::isdigit(c2)) {
   // do whatever you must for invalid inputs
  }
  else {
    invalue = (c1 - '0') * 10 + (c2 - '0');

    switch(invalue) {
      case 0: cmd0();
              break;

      case 3: cmd3();
              break;

       // other valid cases here

      default: // error routine here for invalid input numbers

    }
  }
}
Does this do it?

Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old October 31st, 2004, 07:57 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply Dave. The reason I said I didnt think a switch would work is because I will never be checking just 1 character even when I do the 2 char to int conversion. The whole number will always be '00' not just '0'. Understand what I mean?

Reply With Quote
  #4 (permalink)  
Old November 1st, 2004, 10:24 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I assumed you would have two characters just as you said. I assumed that they were ascii characters obtained from the serial port. I don't know how you actually get the information into your program, so my code snippet assumed that you had already obtained them and their ascii values were in c1 and c2. That is, c1 = '0' and c2 = '3' for example.

Converting the characters c1='0', c2='3' to int results in an integer value of 3. Values of '0' and '0' result in an integer value of 0. The switch is for the integer value of the two characters.

Show me what your data really is (data type and value) and tell me where it came from.

Regards,

Dave
Reply With Quote
  #5 (permalink)  
Old November 2nd, 2004, 04:03 PM
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If speed is less of an issue, you could throw everything into an STL map that would map strings to function/method pointers. By converting everything to a string, you aren't married to the notion of fixed-sized data.

----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
Reply With Quote
  #6 (permalink)  
Old November 2nd, 2004, 04:12 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the replies gentlemen.

Just wanted to update that I am working on a routine using the suggestions you've given and hopefully I will have something to post in a few days. :)

Thanks again

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
lookup value in table Vince_421 Access 16 February 13th, 2007 08:15 AM
Retrieve info about Lookup RowSource for a table c Proteus_3k Pro VB Databases 0 December 2nd, 2004 07:05 AM
Lookup Table Problem mikericc Access VBA 2 January 19th, 2004 07:11 PM
lookup table current field. Squid Access 0 December 21st, 2003 07:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.