| | |
Help with structures
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 19
Reputation:
Solved Threads: 0
I am tring to get the real time data for almost 200 subjects and store it in a file.
But then I get the each subject data in four different subject codes in the sense four rows from the real time data
which I want to store all of it in one row as they all belong to one subjects but they just use different subject codes to get all the data of that particular subject. Even when one part of the subject changes, I want to store the changes in a separate row along with the previous values of the other parts of the subject which haven't changed. So I am using structures for this.
Its working fine for just one subject which has four different codes. But I want to get the data for more than one subject.
For that I have to reset the values of the structure for different subjects. How do I do that??? how to identify the subjectcode for each subject to enter it in the if condition???
Thanks a lot in advance.
But then I get the each subject data in four different subject codes in the sense four rows from the real time data
which I want to store all of it in one row as they all belong to one subjects but they just use different subject codes to get all the data of that particular subject. Even when one part of the subject changes, I want to store the changes in a separate row along with the previous values of the other parts of the subject which haven't changed. So I am using structures for this.
struct subject { string data1; string data2; string data3; } level1, level2, level3; void SubjectData::processMessage( const bha::sessionLayer::SubjectData& event, TibMsg& msg) { if (msg.Get("CODE1", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; code = buffer; found = true; } if (msg.Get("DATA1", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; data1 = buffer; found = true; } if (msg.Get("DATA2", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; data2 = buffer; found = true; } if (msg.Get("DATA3", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; data3 = buffer; found = true; } if (found == true) { if ( code = "SUB1" ) { level1.data1 = data1; level1.data2 = data2; level1.data3 = data3; } if ( code = "SUB2" ) { level2.data1 = data1; level2.data2 = data2; level2.data3 = data3; } if ( code = "SUB3" ) { level3.data1 = data1; level3.data2 = data2; level3.data3 = data3; } std::cout << level1.data1 << ","; std::cout << level1.data2 << ","; std::cout << level1.data3 << ","; std::cout << level2.data1 << ","; std::cout << level2.data2 << ","; std::cout << level2.data3 << ","; std::cout << level3.data1 << ","; std::cout << level3.data2 << ","; std::cout << level3.data3 << std::endl; } }
Its working fine for just one subject which has four different codes. But I want to get the data for more than one subject.
For that I have to reset the values of the structure for different subjects. How do I do that??? how to identify the subjectcode for each subject to enter it in the if condition???
Thanks a lot in advance.
>> buffer[field.Size()] = 0;
this C string technique is unnecessary when using std::string objects (assuming buffer is std::string). And it might actually cause data corruption if the amount of allocated space is not enough for that character.
>> if ( code = "SUB1" )
you probably intend to use the boolean operator ==, not the assignment operator.
I don't know if the following will answer your question, but this is another way to code that function. You really should get rid of those global variables and use class properties instead. That way you can get a separate set of those structures for every instance of SubjectData class.
this C string technique is unnecessary when using std::string objects (assuming buffer is std::string). And it might actually cause data corruption if the amount of allocated space is not enough for that character.
>> if ( code = "SUB1" )
you probably intend to use the boolean operator ==, not the assignment operator.
I don't know if the following will answer your question, but this is another way to code that function. You really should get rid of those global variables and use class properties instead. That way you can get a separate set of those structures for every instance of SubjectData class.
C++ Syntax (Toggle Plain Text)
void SubjectData::processMessage( const bha::sessionLayer::SubjectData& event, TibMsg& msg) { subject* sptr = 0; if (msg.Get("CODE1", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; code = buffer; if(code == "SUB1") sptr = &level1; else if(code == "SUB2") sptr = &level2; else if(code == "SUB3") sptr = &level3; else sptr = 0; // error found = true; } <snip> if (found == true && sptr != 0) { sptr->data1 = data1; sptr->data2 = data2; sptr->data3 = data3; }
Last edited by Ancient Dragon; Dec 21st, 2006 at 9:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
does that mean variables level1, level2, ... level200, levelXXX ? create an array or vector of those structures. Maybe something like this:
C++ Syntax (Toggle Plain Text)
struct subject { string subjectName; string data1; string data2; string data3; } ; // array of structures vector<subject> levels; void SubjectData::processMessage( const bha::sessionLayer::SubjectData& event, TibMsg& msg) { subject oneSubject; if (msg.Get("CODE1", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; oneSubject.subjectName = buffer; } if (msg.Get("DATA1", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; oneSubject.data1 = buffer; } if (msg.Get("DATA2", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; oneSubject.data2 = buffer; } if (msg.Get("DATA3", field) == TIBMSG_OK) { field.Convert(buffer, TIB_BUFFER_SIZE); buffer[field.Size()] = 0; oneSubject.data3 = buffer; } // add the struct to the vector levels.push_back(oneSubject); }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Creating dynamic array structures (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- dynamic array of structures problem (C++)
- C++ structures (C++)
- Read and writing strings from structures (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the C++ Forum
- Previous Thread: missing storage-class or type specifiers error
- Next Thread: I want to know c++
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






