944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 969
  • C++ RSS
Dec 21st, 2006
0

Help with structures

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jobra is offline Offline
19 posts
since Dec 2006
Dec 21st, 2006
0

Re: Help with structures

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

C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Dec 21st, 2006
0

Re: Help with structures

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????
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jobra is offline Offline
19 posts
since Dec 2006
Dec 21st, 2006
0

Re: Help with structures

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)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: missing storage-class or type specifiers error
Next Thread in C++ Forum Timeline: I want to know c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC