Tuesday, July 14, 2009

In C++ how do you convert a string to enum?

Lets say I have the code





Code:





enum sKind_t { S_BLUE, S_GREEN, S_YELLOW };








and





Code:





struct square_t { sKind_t kind; string name; };








And Had a file Like this


BLUE apple


GREEN square


GREEN apple


YELLOW Horse





You then write some code to split up the file into two seperrate strings for each line


example





string A= BLUE


string B = apple





how do you store the enum value via a string


so i want say





square_t.kind = A;


but this doesn't work


how do you get strings that are one of the correct values for an enum to work as an enum category?





you change string A to "S_" + A giving S_BLUE

In C++ how do you convert a string to enum?
As the other answerer has said you cannot directly convert from string to enum. What you can do is have a mapping between the strings and the enum. One simple way would be comparing the inputted string with a colour like BLUE, YELLOW, GREEN. If the string is one of these, then use the enum value of it, like S_BLUE, S_YELLOW, S_GREEN.





I do not understand what you are trying to do, but this may help.
Reply:Enum types are named integer constants. You cannot directly convert them to C-style strings, or vice versa.
Reply:I think you need a dictionary. You can use the STL %26lt;map%26gt; class which operates in logarithmic time or for constant time you can use a hash table.


Visual C++ compiling problem?

I found out how to make a class/struct/union type like "IDD_Cbtncount_Dlg::IDC_CEdit_Display" I try to use this to invoke a property, but when it compiles it says "Syntax error 'semicolin needed before two colins'" I can put a million semicolins in and I still get this. Can you help?

Visual C++ compiling problem?
You'll need to look the code before this function to check is a semicolon is missing or if there is another problem with the code ( which is most likely ).


(C++programing:Structure)Suppo... that in our program, baseball players are defined by the following structure:

Suppose that in our program, baseball players are defined by the following structure:





* struct BaseballPlayer


o string Name


o int atBats


o int runsScored


o double battingAverage


o int RBI





Write a function that takes an array of baseball players, and reports (by returning it) the name of the player with the highest batting average.

(C++programing:Structure)Suppo... that in our program, baseball players are defined by the following structure:
Do your homework roc.
Reply:Good answer Haley.





If you want me to work for you, it's $100/hr.


C++ can someone help with structs?

The question asks the following...Declare a struct cleed subscriber that contains the member's name, streeAddress, monthlyBill (how much subscriber owes), and which paper the subscriber recieves( morning, evening, or both).

C++ can someone help with structs?
struct subscriber {


char* name;


char* streetAddress;


float monthlyBill


char* whichPaper;


};





the char* whichPaper could be replaced with an integer value or an enumeration if you would rather use flags to determine which paper the subscriber uses rather than a string.
Reply:Instantiate and populate...
Reply:this





struct subscriber {


char name[64];


char streetaddress[256];


float monthlybill;


chat type;


};





The last one uses "char"to save the memory.





usage:





subscriber s;


strcpy(s.name,"someone");


strcpy(s.streetaddress,"somewhere");


s.monthlybill=99999.0;


s.type=1; // means "morning"





if





s.type=2; // means "evening"


s.type=3; // means "both"





just like that::)
Reply:Look in your textbook for an example.... this is a really easy thing to do.... just follow the template.





Or do a google search for c++ struct... the top three results are more than ample

survey research

C++ Question?

[code]


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int read ( char , char );





typedef struct


{


int computernum;


char fibit[8];


char sebit[8];


char thbit[8];


char fobit[8];





} address;





main ()


{


char red;


red = read (zero, one);





cout %26lt;%26lt; red %26lt;%26lt; endl;


return 0;


}





int read (char z, char o)


{


ifstream fin;


char zero;


char one;


fin.open ("prog2.dat");





if (fin.fail() )


{


cout %26lt;%26lt; "File not found or opened" %26lt;%26lt; endl;


exit(1);


}





if ( zero = 0)


{


zero= 0;


}


return 0;


}


[/code]





I am lost on the last part mostly. My instructor gave me a file named prog2.dat which contained this file





"


00001110011110010000101001011010


00000011000000110000011000000110


10010010001111110101101001010101


00000001000000010000000100000001


00000111001010111101010101110101


00000000101011110101010101110

C++ Question?
since int read(..) return only zero .....looks like your program will only output zeros....


so what is your question?
Reply:You haven't asked any question. All you've done is post some source code and input file info. What is the code supposed to do? What are you having trouble figuring out?


C programming question?

If I want to read a list of 3 numbers delimited by a comma into a structure... what would be the best way of doing it?





EX.





struct Numbers{


int x;


int y;


iny z;


} my_nums;





and my input on one line is 5.4, 56.34, 12.6





How best can I get those data values into my stucture?

C programming question?
Use fgets to grab the input into a C string. Then, use strtol on this string three times. That is, your first call to strtol should get you your first number. You get a pointer to the comma, so advance the pointer by one and call strtol on the new boundaries once again. And so on.





http://cppreference.com in case you don't know where to find the functions. Google gets you Dinkumware's reference as well.
Reply:typedef struct my_nums *vals;





scanf("%f %f %f ", vals-%26gt;x, vals-%26gt;y, vals-%26gt;z);





I'm not sure about the scanf flags and conversions, but this should be (a bit) of a start.


C pr0gramming?

Given the following declaration:





#define MAX 100





typedef char string8[9];


typedef char string30[31] ;





typedef struct


{ string8 strId;


string30 strName;


string8 strCourse;


} structIDRecType;


typedef structIDRecType arrID[MAX];





(1) Write function sortArr that will arrange the contents of the


array in ascending order according to its strId. Note that the


contents of the structures need not be unique. This means that you


may have several copies of the same structure. Furthermore, you are


not allowed to introduce additional arrays. Use the function


heading below:


void sortArr (arrID aId)

C pr0gramming?
Please, just do your own homework. They only give you homework to help you - not to waste your time. By "cheating the system" and getting someone else to do the work for you, you are only cheating yourself out of an education. If you don't want to learn then quit your course and let somebody more deserving have your place.





Rawlyn.
Reply:You seriously need to check your codeing, because just taking one look at it makes me think that there is something wrong with it so, just make sure that it is correct.
Reply:Sorry bro... i'm not C Programmers :P,


I Agree with Rawlyn