I was wondering how you could combine fixed strings and variables into one variable.
This is a discussion on Newbie strings question within the C++ Programming forums, part of the General Programming Boards category; I was wondering how you could combine fixed strings and variables into one variable....
I was wondering how you could combine fixed strings and variables into one variable.
You can just string streams to do such a task. Note that this might not work with ancient compilers, but it's standard as of 97.
Code:#include <sstream> #include <string> #include <iostream> #include <cmath> int main() { float pi = 3.14159265; double e = exp(1); int twentyThree = 23; char charArray[] = "deprecated"; char theLetterB = 'b'; std::ostringstream outStream; outStream << pi << " " << e << " " << twentyThree << " " << charArray << " " << theLetterB << " more stuff\n"; std::string allOfEmCombined = outStream.str(); std::cout << allOfEmCombined; return 0; }
thanks