Getting the same value everytime??
-
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:) -
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:)Probably because you are always looking at the same offset into the RFMAccess::TSimHeader_arr and RFMAccess::TSimSignal_arr structures - zero!
-
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:)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)
-
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:)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"