Help with structures

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Help with structures

 
0
  #1
Dec 21st, 2006
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.

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with structures

 
0
  #2
Dec 21st, 2006
>> 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.

  1. void SubjectData::processMessage(
  2. const bha::sessionLayer::SubjectData& event, TibMsg& msg) {
  3.  
  4. subject* sptr = 0;
  5.  
  6. if (msg.Get("CODE1", field) == TIBMSG_OK) {
  7. field.Convert(buffer, TIB_BUFFER_SIZE);
  8. buffer[field.Size()] = 0;
  9. code = buffer;
  10. if(code == "SUB1")
  11. sptr = &level1;
  12. else if(code == "SUB2")
  13. sptr = &level2;
  14. else if(code == "SUB3")
  15. sptr = &level3;
  16. else
  17. sptr = 0; // error
  18. found = true;
  19. }
  20. <snip>
  21.  
  22. if (found == true && sptr != 0)
  23. {
  24. sptr->data1 = data1;
  25. sptr->data2 = data2;
  26. sptr->data3 = data3;
  27. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 19
Reputation: jobra is an unknown quantity at this point 
Solved Threads: 0
jobra jobra is offline Offline
Newbie Poster

Re: Help with structures

 
0
  #3
Dec 21st, 2006
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????
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with structures

 
0
  #4
Dec 21st, 2006
does that mean variables level1, level2, ... level200, levelXXX ? create an array or vector of those structures. Maybe something like this:
  1. struct subject
  2. {
  3. string subjectName;
  4. string data1;
  5. string data2;
  6. string data3;
  7. } ;
  8. // array of structures
  9. vector<subject> levels;
  10.  
  11. void SubjectData::processMessage(
  12. const bha::sessionLayer::SubjectData& event, TibMsg& msg) {
  13.  
  14. subject oneSubject;
  15.  
  16. if (msg.Get("CODE1", field) == TIBMSG_OK) {
  17. field.Convert(buffer, TIB_BUFFER_SIZE);
  18. buffer[field.Size()] = 0;
  19. oneSubject.subjectName = buffer;
  20. }
  21.  
  22. if (msg.Get("DATA1", field) == TIBMSG_OK) {
  23. field.Convert(buffer, TIB_BUFFER_SIZE);
  24. buffer[field.Size()] = 0;
  25. oneSubject.data1 = buffer;
  26. }
  27.  
  28. if (msg.Get("DATA2", field) == TIBMSG_OK) {
  29. field.Convert(buffer, TIB_BUFFER_SIZE);
  30. buffer[field.Size()] = 0;
  31. oneSubject.data2 = buffer;
  32. }
  33.  
  34.  
  35. if (msg.Get("DATA3", field) == TIBMSG_OK) {
  36. field.Convert(buffer, TIB_BUFFER_SIZE);
  37. buffer[field.Size()] = 0;
  38. oneSubject.data3 = buffer;
  39. }
  40. // add the struct to the vector
  41. levels.push_back(oneSubject);
  42. }
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC