Re: Enum and string (again)

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 06/14/04


Date: Mon, 14 Jun 2004 20:43:57 +1000


"KPR" <kpruppel@optonline.net> wrote in message news:40CD240C.7010709@optonline.net...
| I'm trying to take a string (either 1 or 2 characters) and use an enum
| to get an integer that I can then use as a subscript in a Totals array.
| Therefore, this hangs on converting a string to the integral constant in
| the enum list, which then lets me get my hands on a "real" integer to
| use for a subscript.
|
| I'm still baffled. I got some replies last week, and the one I tried
| implementing was from Thomas Matthews. Here's what he gave me.
|
| ---------------------------------------------------------------------------------------------------------------
| enum Meeting_Type {B,BB,C,CD,L,K,O,OD,P,S,T,To,W} mt;
| static const string meeting_names[]=
|
| {string("B"),string("BB"),string("C"),string("CD"),string("L"),string("M"),
|
| string("O"),string("OD"),string("P"),string("S"),string("T"),string("To"),
| string("W")};
| string Get_Meeting_Type_Name(Meeting_Type mt)
| {
| return meeting_names[mt];
| }
| ---------------------------------------------------------------------------------------------------------------
| DDRlist is the input vector<string>. Two sample records are "0un0745B"
| and "4hu0900To".
| This is as far as I get. No matter where or how I place the above
| function, I still get a string in return. Somehow, there has got to be
| a way to pass the string and get the Meeting_Type mt returned, but I
| just don't see it. I have tried many combinations, all no good.
|
| int main() { /* .....................*/
| int Totals[14]={0};
| for (int i=0; i <= DDRlist.size(); i++)
| if (DDRlist[i].size() == 8) {
| string Mtgcode=DDRlist[i].substr(7,1);
| ????????????
| } // end if
| }; // end main

What Thomas presented, is what he believed you were asking
for(given your description). It sounds to me like you want
the opposite ?. If so, then use something the following:

enum Meeting_Type{ B, BB, C, CD, L, K, O, OD, P, S, T, To, W };

inline int GetMeetingType( const std::string& Key )
 {
  std::map<std::string, Meeting_Type> MT;

  MT[ "B" ] = B; MT[ "BB" ] = BB; MT[ "C" ] = C;
  MT[ "CD" ] = CD; MT[ "L" ] = L; MT[ "K" ] = K;
  MT[ "O" ] = O; MT[ "OD" ] = OD; MT[ "P" ] = P;
  MT[ "S" ] = S; MT[ "T" ] = T; MT[ "To" ] = To;
  MT[ "W" ] = W;

  return MT[ Key ];
 }

There is an implicit conversion from 'Meeting_Type' to 'int'
in the function return expression.

Cheers.
Chris Val



Relevant Pages

  • Re: Date validation function issue
    ... > then concatenating each into one larger string and converting into an int. ... be able to handle nine-plus digits. ...
    (comp.lang.c)
  • Re: Integers have docstring for int()
    ... > for int being the whole class. ... The following seems to be unaccustomed, but it's good Python: ... Convert a string or number to an integer, ... When converting a string, use ...
    (comp.lang.python)
  • Re: CR & Tab in VB 2005 Textbox
    ... Secondly the CStr is converting as you saw to a string with that value. ... I do this since I use both VB and C# and sometimes I need to use code and if I use the Dot.Net functions provided by the objects rather than the VB it is easier to convert since it is mostly getting rid of ending semi colons and changing square brackets into regular brackets. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Promoting unsigned long int to long int
    ... I wrote a function for converting a string of doubles of the form ... zzzzz" into three seperate doubles. ... int main ...
    (comp.lang.c)
  • Re: Unicode String Parameters in DLLs
    ... > NOT Unicode, so you might have heard some stuff about that. ... > One thing to watch out for is if your DLL routine puts a zero byte at the ... > string ); it does not terminate the string. ... >> We are converting our application to work with Unicode. ...
    (microsoft.public.word.vba.general)