Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting the same value everytime??

Getting the same value everytime??

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresregexhelpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    I am trying to spin through an array in a struct and for each element in that array I want to get the value of another array in another struct. The problem I am having is that the value is always the same. My structs are: struct TSimHeader { char Name[45][20]; char Unit[45][20]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[10]; struct TSimSignal { double Value[45]; double TimeStamp; }static TSimSignal_arr[10]; My function to match elements in the TSimHeader_arr[0].Name[] array with the same element in the TSimSignal_arr[0].Value[] is: **void Tflight_data::getValues(){ int numSignals = RFMAccess::TSimHeader_arr[0].SignalCount; for(int i=0; i<numSignals; i++){ char* temp = RFMAccess::TSimHeader_arr[0].Name[i]; char* temp2 = RFMAccess::TSimHeader_arr[0].Name[i+1]; if(strcmp(temp, "pla")) display_pla_level(RFMAccess::TSimSignal_arr[0].Value[i]); if(strcmp(temp, "pitch_stickF")&& strcmp(temp2, "roll_stickF")){ roll = (RFMAccess::TSimSignal_arr[0].Value[i] / 10.0); pitch = (RFMAccess::TSimSignal_arr[0].Value[i+1] / 10.0); flight_data->fwd_pl->Repaint(); } if(strcmp(temp, "nose_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) front_wheel_led->Active = true; } if(strcmp(temp, "left_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) left_wheel_led->Active = true; } if(strcmp(temp, "right_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) right_wheel_led->Active = true; } if(strcmp(temp, "pitch_trim")) pitch_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "roll_trim")) roll_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "yaw_trim")) yaw_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; } }** The values should be different depending on the word in the char array. It should match with the exact element in the Values array. Any ideas why I am getting the same value on each if statement? I hope I explained it well enough. Thanks, steven:)

    D P R 3 Replies Last reply
    0
    • J johnstonsk

      I am trying to spin through an array in a struct and for each element in that array I want to get the value of another array in another struct. The problem I am having is that the value is always the same. My structs are: struct TSimHeader { char Name[45][20]; char Unit[45][20]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[10]; struct TSimSignal { double Value[45]; double TimeStamp; }static TSimSignal_arr[10]; My function to match elements in the TSimHeader_arr[0].Name[] array with the same element in the TSimSignal_arr[0].Value[] is: **void Tflight_data::getValues(){ int numSignals = RFMAccess::TSimHeader_arr[0].SignalCount; for(int i=0; i<numSignals; i++){ char* temp = RFMAccess::TSimHeader_arr[0].Name[i]; char* temp2 = RFMAccess::TSimHeader_arr[0].Name[i+1]; if(strcmp(temp, "pla")) display_pla_level(RFMAccess::TSimSignal_arr[0].Value[i]); if(strcmp(temp, "pitch_stickF")&& strcmp(temp2, "roll_stickF")){ roll = (RFMAccess::TSimSignal_arr[0].Value[i] / 10.0); pitch = (RFMAccess::TSimSignal_arr[0].Value[i+1] / 10.0); flight_data->fwd_pl->Repaint(); } if(strcmp(temp, "nose_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) front_wheel_led->Active = true; } if(strcmp(temp, "left_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) left_wheel_led->Active = true; } if(strcmp(temp, "right_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) right_wheel_led->Active = true; } if(strcmp(temp, "pitch_trim")) pitch_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "roll_trim")) roll_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "yaw_trim")) yaw_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; } }** The values should be different depending on the word in the char array. It should match with the exact element in the Values array. Any ideas why I am getting the same value on each if statement? I hope I explained it well enough. Thanks, steven:)

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Probably because you are always looking at the same offset into the RFMAccess::TSimHeader_arr and RFMAccess::TSimSignal_arr structures - zero!

      1 Reply Last reply
      0
      • J johnstonsk

        I am trying to spin through an array in a struct and for each element in that array I want to get the value of another array in another struct. The problem I am having is that the value is always the same. My structs are: struct TSimHeader { char Name[45][20]; char Unit[45][20]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[10]; struct TSimSignal { double Value[45]; double TimeStamp; }static TSimSignal_arr[10]; My function to match elements in the TSimHeader_arr[0].Name[] array with the same element in the TSimSignal_arr[0].Value[] is: **void Tflight_data::getValues(){ int numSignals = RFMAccess::TSimHeader_arr[0].SignalCount; for(int i=0; i<numSignals; i++){ char* temp = RFMAccess::TSimHeader_arr[0].Name[i]; char* temp2 = RFMAccess::TSimHeader_arr[0].Name[i+1]; if(strcmp(temp, "pla")) display_pla_level(RFMAccess::TSimSignal_arr[0].Value[i]); if(strcmp(temp, "pitch_stickF")&& strcmp(temp2, "roll_stickF")){ roll = (RFMAccess::TSimSignal_arr[0].Value[i] / 10.0); pitch = (RFMAccess::TSimSignal_arr[0].Value[i+1] / 10.0); flight_data->fwd_pl->Repaint(); } if(strcmp(temp, "nose_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) front_wheel_led->Active = true; } if(strcmp(temp, "left_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) left_wheel_led->Active = true; } if(strcmp(temp, "right_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) right_wheel_led->Active = true; } if(strcmp(temp, "pitch_trim")) pitch_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "roll_trim")) roll_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "yaw_trim")) yaw_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; } }** The values should be different depending on the word in the char array. It should match with the exact element in the Values array. Any ideas why I am getting the same value on each if statement? I hope I explained it well enough. Thanks, steven:)

        P Offline
        P Offline
        Peter Weyzen
        wrote on last edited by
        #3

        see every occurrance of "TSimSignal_arr[0]." I'd also consider cleaning up your data structures. I've been doing this for 10+ years, and I still can't stand doubly indexed (2d) arrays used for characters.... not intuitive at all. struct TSimHeader {   char Name[45][20];   char Unit[45][20];   double Min[45];   double Max[45];   int SignalCount;   int SimStatus; }static TSimHeader_arr[10]; struct TSimSignal {   double Value[45];   double TimeStamp; }static TSimSignal_arr[10]; couldn't this be something more like: #define MAX_ENTRIES (45) #define MAX_ARRAY_SIZE (10) #define MAX_STRING_SIZE (20) struct TSimHeaderEntry {   char Name[MAX_STRING_SIZE];   char Unit[MAX_STRING_SIZE];   double Min;   double Max; }; struct TSimHeader {   TSimHeaderEntry Entries[MAX_ENTRIES];   int SignalCount;   int SimStatus; }static TSimHeader_arr[MAX_ARRAY_SIZE]; struct TSimSignal {   double Value[MAX_ENTRIES];   double TimeStamp; }static TSimSignal_arr[MAX_ARRAY_SIZE]; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [Santa Cruz Networks](http://www.santacruznetworks.com)

        1 Reply Last reply
        0
        • J johnstonsk

          I am trying to spin through an array in a struct and for each element in that array I want to get the value of another array in another struct. The problem I am having is that the value is always the same. My structs are: struct TSimHeader { char Name[45][20]; char Unit[45][20]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[10]; struct TSimSignal { double Value[45]; double TimeStamp; }static TSimSignal_arr[10]; My function to match elements in the TSimHeader_arr[0].Name[] array with the same element in the TSimSignal_arr[0].Value[] is: **void Tflight_data::getValues(){ int numSignals = RFMAccess::TSimHeader_arr[0].SignalCount; for(int i=0; i<numSignals; i++){ char* temp = RFMAccess::TSimHeader_arr[0].Name[i]; char* temp2 = RFMAccess::TSimHeader_arr[0].Name[i+1]; if(strcmp(temp, "pla")) display_pla_level(RFMAccess::TSimSignal_arr[0].Value[i]); if(strcmp(temp, "pitch_stickF")&& strcmp(temp2, "roll_stickF")){ roll = (RFMAccess::TSimSignal_arr[0].Value[i] / 10.0); pitch = (RFMAccess::TSimSignal_arr[0].Value[i+1] / 10.0); flight_data->fwd_pl->Repaint(); } if(strcmp(temp, "nose_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) front_wheel_led->Active = true; } if(strcmp(temp, "left_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) left_wheel_led->Active = true; } if(strcmp(temp, "right_wow")){ if(RFMAccess::TSimSignal_arr[0].Value[i] != 0) right_wheel_led->Active = true; } if(strcmp(temp, "pitch_trim")) pitch_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "roll_trim")) roll_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; if(strcmp(temp, "yaw_trim")) yaw_data_lb->Caption = (AnsiString)RFMAccess::TSimSignal_arr[0].Value[i]; } }** The values should be different depending on the word in the char array. It should match with the exact element in the Values array. Any ideas why I am getting the same value on each if statement? I hope I explained it well enough. Thanks, steven:)

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          strcmp() returns 0 if the strings are the same, non-zero if they are different. Your if statements will always be executed if the strings are not the same, which I assume is the majority of times. It looks like you want the if statements to be executed if the strings are equal, so you'll need to modify them like this:

          if(strcmp(temp, "pla") == 0)

          etc... Hope this helps,

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups