-
dynamic strings
In web programming langauges like php, you can put variable names in your string declaration like this:
var location = "Spain"
var myString = "The rain in $location is falling on the plain"
echo $myString; // outputs "The rain in Spain is falling on the plain"
I was wondering if there's something similar to this you can do with c++?
Thanks,
Ham
-
I'm sure you could overload the << operator for your new string type to achieve that.
-
-
Code:
#include <iostream> //basic io
#include <string> //obvious
using namespace std;
int main()
{
string location = "Spain";
string my_string = "The rain in "+ location +" is falling on the plain";
cout<<my_string;
cin.get();
return 0;
}
-
-
BTW, that will only work for strings. If you want to add other variable types to the string (like numbers), you should look up stringstreams.
-
I got around the problem using printf instead because the following wouldn't work:
Code:
#include <iostream> //basic io
#include <string> //obvious
using namespace std;
int main()
{
string location;
string my_string = "The rain in "+ location +" is falling on the plain";
location = "Spain"
cout<<my_string; // The rain in is falling on the plain
cin.get();
return 0;
}
-
define locations first:
Code:
#include <iostream> //basic io
#include <string> //obvious
using namespace std;
int main()
{
string location;
location = "Spain"
string my_string = "The rain in "+ location +" is falling on the plain";
cout<<my_string; // The rain in is falling on the plain
cin.get();
return 0;
}
-
IMO, using tools from a different language just because you made a small mistake in the current one doesn't make a whole lot of sense.
Don't forget the semi-colon after you assign Spain to location as well.
-
It's not a mistake; I have all my string declarations at the beginning of the program, and those variables (e.g. location) are not set until further down the program during run-time.
I found a solution by using printfs instead:
Code:
char * my_string = "The rain in %s is falling on the plain";
// more code here
// more code here
// more code here
// more code here
char * location = "Spain";
printf (my_string, location);
I have no idea why the C++ syntaxes can't get this to work the way I want.
-
I don't understand why you think it wasn't a mistake. You posted the code that doesn't work, and it doesn't work because it is wrong. The code is right there and mrafcho001 indicated why it didn't work. Your C version would have failed as well if you had made the same mistake there.
At the time you used the location variable, it was empty, so that is why it didn't work.
If your code is more complicated, and you are having trouble translating the example to work for you, feel free to post more of the actual code so we can help you fix it (assuming you want to try and get it to work in C++).
-
I don't think that any language can do what you want.
Code:
string location; // this is an empty string
string my_string = "The rain in "+ location +" is falling on the plain";
location = "Spain"; // now it gets a value
cout<<my_string; // now you expect mystring to mysteriously change to
// include the new value of location
-
[sigh]
People can't even copy and paste nowadays
[/sigh]
;)
-
regex
Hello all, I'm pretty new to c++ so correct me where I'm wrong. In vb.net I would use regular expressions to do it. I would make a function that you input the string and a collection of replacments possibly like a hashtable with the string to replace as the key and the value as the replacement string. you could then set the value of the string any where so long as you cout the result of the function instead of the original string. I don't know how to do regular expressions or hashtables in c++ yet but I would think that you could.
-
If you're totally new to c++ then it wouldn't hurt looking over the tutorials on this board...
You're experience in VB will help.
Another thing, you need to start a new thread if you wish to discuss another topic.