Using variables in a string

Hello ~

I am trying to set the text in an edit box in my GUI app to some JavaScript code. but with all the parameters as variables, e.g.

User sets a value in a Dialog
Variable is assigned to value in Dialog
Edit control's text is assigned to "string" [variable] "more string"

How would I achieve this? The string is a character array.

Thanks;

~ Christopher Howarth
[426 byte] By [chrishowarth] at [2007-11-20 9:18:39]
# 1 Re: Using variables in a string
use a stringstream.
e.g.
#include <iostream>
#include <sstream>
using namespace std;

int main() {
stringstream str;
int i = 7;
str << "some text a var " << i << " and more_text";
const char * ch = str.str().c_str();
cout << ch << endl;
}
Kurt
ZuK at 2007-11-9 1:21:49 >
# 2 Re: Using variables in a string
Another way is with sprintf:

#include <cstdio>

..

int myIntVar = dialog.getAValue();
char * myStrVar = dialog.getAnotherValue();

char output[100];

sprintf(output, "I would like an int here: %d and a string here: %s",
myIntVar, myStrVar);

If you've never seen any of the printf-type functions, here's a quick reference: http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

In short, whenever you put a % it means that you replace that spot with a variable in the list after the string you put in. %d is for integers (d for decimal), %c is chars, %f for floats, %s for strings, etc. If you want to put an actual % sign you just put two of them: printf("100%%") outputs 100%.
IllegalCharacter at 2007-11-9 1:22:47 >
# 3 Re: Using variables in a string
I understand how to do it with cout and prinf family, I don't understand how to do it with just two quote marks. I'm using SendMessage() as part of the WinAPI for GUI apps, but as my question isn't directly related to the API I thought that I ought to post here. The source for what I am doing is here:

SendMessage(
(HWND) hEdit, //Irrelevant
(UINT) WM_SETTEXT, //Irrelevant
(WPARAM) NULL, //Irrelevant
(LPARAM)"Begin String..." I want my variable here "...End String");//<-String

Sorry about being unclear.

Chris Howarth
chrishowarth at 2007-11-9 1:23:43 >
# 4 Re: Using variables in a string
I understand how to do it with cout and prinf family, I don't understand how to do it with just two quote marks. I'm using SendMessage() as part of the WinAPI for GUI apps, but as my question isn't directly related to the API I thought that I ought to post here. The source for what I am doing is here:

SendMessage(
(HWND) hEdit, //Irrelevant
(UINT) WM_SETTEXT, //Irrelevant
(WPARAM) NULL, //Irrelevant
(LPARAM)"Begin String..." I want my variable here "...End String");//<-String

Sorry about being unclear.

Chris Howarth
Just incorporate what Kurt (ZuK) wrote into your code:
#include <iostream>
#include <sstream>
using namespace std;

int main() {
stringstream str;
int i = 7;
str << "Begin String..." << yourVar << "...End String";
const char * ch = str.str().c_str();
SendMessage(
(HWND) hEdit, //Irrelevant
(UINT) WM_SETTEXT, //Irrelevant
(WPARAM) NULL, //Irrelevant
(LPARAM)ch)
}
Viggy
MrViggy at 2007-11-9 1:24:48 >
# 5 Re: Using variables in a string
Ah OK. Thanks!
chrishowarth at 2007-11-9 1:25:55 >
# 6 Re: Using variables in a string
If you want to use the format:

"Begin String..." + v + "...End String"

in one go you can put in a wrapper to v so make_string(v) which return std::string, which supports operator+ both to the left and right.

A standard way to implement make_string might be:

template < typename T >
std::string make_string( const T& t )
{
std::ostringstream oss;
oss << t;
return oss.str();
}

You can of course expand this to do all sorts of formatting.

Note that building strings this way is not likely to be the most efficient way. There are implementations of a "rope" class which is more optimal for string-building in this manner.
SGI-STL has a rope class.

However only put performance in if you need it. Much of the time you won't.
NMTop40 at 2007-11-9 1:26:54 >