954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with structures

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.

<strong>struct</strong> subject
{
     string data1;
     string data2;
     string data3;
} level1, level2, level3; 

<strong> 
void</strong> SubjectData::processMessage(
<strong>const</strong> bha::sessionLayer::SubjectData& event, TibMsg& msg) {

 
<strong>if</strong> (msg.Get("CODE1", field) == TIBMSG_OK) {
    field.Convert(buffer, TIB_BUFFER_SIZE);
    buffer[field.Size()] = 0;
    code = buffer;
    found = <strong>true</strong>;
}

 
<strong>if</strong> (msg.Get("DATA1", field) == TIBMSG_OK) {
   field.Convert(buffer, TIB_BUFFER_SIZE);
   buffer[field.Size()] = 0;
   data1 = buffer;
   found = <strong>true</strong>;
}

 
<strong>if</strong> (msg.Get("DATA2", field) == TIBMSG_OK) {
   field.Convert(buffer, TIB_BUFFER_SIZE);
   buffer[field.Size()] = 0;
   data2 = buffer;
   found = <strong>true</strong>;
}
 

<strong>if</strong> (msg.Get("DATA3", field) == TIBMSG_OK) {
   field.Convert(buffer, TIB_BUFFER_SIZE);
   buffer[field.Size()] = 0;
   data3 = buffer;
   found = <strong>true</strong>;
}
 

<strong>if</strong> (found == <strong>true</strong>)
{
<strong>   if</strong> ( code = "SUB1" )
  {
       level1.data1 = data1;
       level1.data2 = data2;
       level1.data3 = data3;
  }
<strong>   if</strong> ( code = "SUB2" )
  {
        level2.data1 = data1;
        level2.data2 = data2;
        level2.data3 = data3;
  }
<strong>   if</strong> ( 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 finefor 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.

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

>> 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.

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;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanxs a lot for that. That does help but still the coeds SUB1, SUB2 & SUB3 is all part of one subject. What if another subject has codes TAB1, TAB2 & TAB3 and another subject has codes BOB1, BOB2 & BOB3.....like that I have 200 subjects. How do I do it????

jobra
Newbie Poster
19 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

does that mean variables level1, level2, ... level200, levelXXX ? create an array or vector of those structures. Maybe something like this:

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); 
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You