replacing a character in a string but doing it inline?

I have a series of lines of code, all that have one value that I need to replace with an integer variable. I know there's strcat, or sprintf, but what I'd like to do is replace this single character inline if possible.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int pLayer, pCloudType, pCloudBase, pCloudTops, pWindMSL, pWindKts, pWindShearDir, pWindShearKts, pTurbulence;
float pWindDir;
					
sscanf(IOSCommand.c_str(), "%i %i %i %i %i %f %i %i %i %i", &pLayer, &pCloudType, &pCloudBase, &pCloudTops, &pWindMSL, &pWindDir, &pWindKts, pWindShearDir, pWindShearKts, pTurbulence);
					
XPLMSetDatai(XPLMFindDataRef("sim/weather/cloud_type[0]"), pCloudType);
XPLMSetDatai(XPLMFindDataRef("sim/weather/cloud_base_msl_m[0]"), pCloudBase * 0.3048);
XPLMSetDatai(XPLMFindDataRef("sim/weather/cloud_tops_msl_m[0]"), pCloudTops * 0.3048);
XPLMSetDatai(XPLMFindDataRef("sim/weather/wind_altitude_msl_m[0]"), pWindMSL * 0.3048);
XPLMSetDataf(XPLMFindDataRef("sim/weather/wind_direction_degt[0]"), pWindDir);
XPLMSetDatai(XPLMFindDataRef("sim/weather/wind_speed_kt[0]"), pWindKts);
XPLMSetDatai(XPLMFindDataRef("sim/weather/shear_direction_degt[0]"), pWindShearDir);
XPLMSetDatai(XPLMFindDataRef("sim/weather/shear_speed_kt[0]"), pWindShearKts);
XPLMSetDatai(XPLMFindDataRef("sim/weather/turbulence[0]"), pTurbulence);


You can see the [0] in each of the Dataref names...I'd like to replace the 0 with the value of pLayer...as simply as possible for all 9 of those Set commands...any quick and easy ideas?

Thanks all!
For example, you can use sprintf

char s[100];

sprintf( s, "sim/weather/cloud_type[%u]", pLayer );

Or can use snprintf if your C-compiler supports the C99 Standard.
Last edited on
Problem with that is the fact that it writes the result to s. Not inline right? This method would require an additional line for each dataref right? Any way to do it all in one line for each one?
You may not change a string literal. So in any case you should use any other object.
Last edited on
XPLMFindDataRef(("sim/weather/cloud_type["+std::to_string(pLayer)+"]").c_str())
Assuming a C++11 compiler is available. If not, use boost::lexical_cast<string> instead.
@Athar
I am afraid that this has undefined behavior because the temporary object will be deleted. So you pass a pointer to a temporary object.
Last edited on
Of course it is deleted, but only after the function call has been made.
@Athar,
thanks
Thanks, I'll try this tomorrow when I get back to work.
I'm running VS 2010, so I don't think that's a 11 compiler right?

I do the above and I get:

1 IntelliSense: more than one instance of overloaded function "to_string" matches the argument list: s:\documents\visual studio 2010\projects\sd_ios\sd_ios.cpp 386 61 SD_IOS

Any ideas on how to proceed?
Write the following way

XPLMFindDataRef(("sim/weather/cloud_type["+std::to_string( ( long long )pLayer)+"]").c_str())
Topic archived. No new replies allowed.