Hello,
I want to convert this java statement to C++:
String a;
int b=1;
double c=2.32;
String d="abc";
a=b + c + d;
Thank you very much!
Hello,
I want to convert this java statement to C++:
String a;
int b=1;
double c=2.32;
String d="abc";
a=b + c + d;
Thank you very much!
An easy way is sprintf()
char a[] = "These numbers to follow";
int b=1;
double c=2.32;
char d[255];
sprintf(d,"%s%i%f",a,b,c);
cout << d;
Thank you very much.
Can sprintf support string?
May I change to:
string a= "These numbers to follow";
int b=1;
double c=2.32;
string d;
sprintf(d,"%s%i%f",a,b,c);
cout << d;
The main language doesnt have a string data type. You can have a wrapper class with the operators overloaded to mimic a string, but the data of this type is always held in a charector array as shown above.
I wouldnt bother with string wrappers to much at this stage, leave them for when your happier with the C language.
ANSI C++ has a string data type ready to use. Besides, we also have string streams, that would fit exactly what you want to do. Here is a sample:
RayonCode:#include <iostream> #include <string> #include <sstream> using namespace std; int main() { char a[] = "These numbers to follow"; int b=1; double c=2.32; char d[255]; ostringstream oss; oss << a << b << c; cout << oss.str() << endl; // Optionally ... string e = oss.str(); // Play with string e here before printing out cout << e << endl; }