|
Subject:
|
Lookup (Dispatch) Table
|
|
Posted By:
|
CNewbie
|
Post Date:
|
10/31/2004 5:57:15 PM
|
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 By:
|
davekw7x
|
Reply Date:
|
10/31/2004 6:51:21 PM
|
If you have two ascii characters, say c1 and c2, just convert to an int and do the switch:
#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 By:
|
CNewbie
|
Reply Date:
|
10/31/2004 6:57:41 PM
|
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 By:
|
davekw7x
|
Reply Date:
|
11/1/2004 9:24:57 AM
|
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 By:
|
klep
|
Reply Date:
|
11/2/2004 3:03:39 PM
|
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 By:
|
CNewbie
|
Reply Date:
|
11/2/2004 3:12:14 PM
|
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
|