Variables within strings? (C++)

Started by
22 comments, last by ifthen 11 years, 4 months ago
I'm just going to get straight to the point:
Using "cout", I can get text, then a variable, then some more text. I wondered if I could replicate that into a string?
e.g
cout << "Hi, I am " << x << "years old."; into a string.

EDIT: I've tried something with a string, but I've gotten a compile error:
invalid operands of types `const char[22]' and `int' to binary `operator<<'

The string is as follows:
string day1_c_1 = "You, along with your " << rs_q_c << " friends, set up camp outside of town. You carried " << rs_q_f << " units of food with you, along with another " << rs_q_m << " units of metal.";
Advertisement
I might have misunderstood your question.. But are you asking for a way to concatenate strings and variables together? If so : yes you can!
If the variable is of type string you can do :

std::string s = "hi" + yourstring;


Otherwise .. If it is a number you have to cast it to a string before.

EDIT : the c++ way to convert numbers to integers is the stringstream (just google it ). I'm writing from phone :(
string day1_c_1 = "You, along with your " << rs_q_c << " friends, set up camp outside of town. You carried " << rs_q_f << " units of food with you, along with another " << rs_q_m << " units of metal.";

The variables rs_q_letter is a number. I'm wondering if I could pack them all into a sentence like the above, which produces a compile error, or do I have to do each quote separately?
So instead of the above, I'd have to do:

string day1_c_1 = "You, along with your ";
string day1_c_2 = " friends, set up camp outside of town. You carried ";
string day1_c_3 = " units of food with you, along with another ";
string day1_c_4 = " units of metal.";

And then print it out like this:

cout << day1_c_1 << rs_q_c << day1_c_2 << rs_q_f << day1_c_3 << rs_q_m << day1_c_4


EDIT:
Variables:
int rs_q_f = 0; //Food
int rs_q_m = 0; //Metal
int rs_q_c = 0; //Companions
.
Which compiler are you using?
If it's newest GCC or Visual Studio 2012 you should have to_string function:

string text = day1_c_1 + to_string(rs_q_c) + day1_c_2 + to_string(...) + ...;
There is a C++ type for doing exactly this. Its name is std::stringstream. Usage example:
[source lang="cpp"]#include <sstream>
//declare it somewhere
std::stringstream ss; //internal string empty at beginning
//usage
ss << "You, along with your " << numFriends << " friends, set up camp..."; //append this to the internal string
std::string setUpCampLabel = ss.str(); //get a copy of stringbuilder internal string
ss.clear(); //clear the internal string
ss << "You were eaten by a bear!";
std::cout << setUpCampLabel << std::endl <<ss.str(); //it is possible to print the internal string directly[/source]
#include <sstream>
//declare it somewhere
std::stringstream ss; //internal string empty at beginning
//usage
ss << "You, along with your " << numFriends << " friends, set up camp..."; //append this to the internal string
std::string setUpCampLabel = ss.str(); //get a copy of stringbuilder internal string
ss.clear(); //clear the internal string
ss << "You were eaten by a bear!";
std::cout << setUpCampLabel << std::endl <<ss.str(); //it is possible to print the internal string directly

How is this any more efficient than just putting:
cout << "You, along with your " << numFriends << " friends, set up camp..." << endl << "You were eaten by a bear!"
I'm also using DevC++, not Visual Studio.
So, in short, you simply can't declare a string and put an integer variable within it? You have to do that?

  1. You should Google "Why you shouldn't use Dev-C++".
  2. More lines != less efficient
  3. String and integer are very different, so you have to convert it somehow, stringstream is one of ways. Even if other languages allow to simply put variable name in the string, internally it still does something similar to this.


You can create function that'll convert it:

template<typename T>
string to_string(T v) {
stringstream str;
str << v;
return str.str();
}
Sorry! I got std::to_int() mixed up with std::to_string! Please forgive me :)!

For all intensive purposes, std::to_int doesn't exist and please disregard my old comment.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

There's no such thing as std::to_int(). The closest thing in behavior would be std::stoi(), the closest thing in name would be the to_int_type() member function of the char traits classes.

This topic is closed to new replies.

Advertisement