I don't know how to convert an int to a sting in C++ and it is driving me nuts. I know this is easy. Can someone PLEASE help?
I don't know how to convert an int to a sting in C++ and it is driving me nuts. I know this is easy. Can someone PLEASE help?
Code:#include <iostream> #include <sstream> using namespace std; int main () { int val; string mystr; stringstream ss (stringstream::in | stringstream::out); ss << "120 42 377 6 5 2000"; for (int n=0; n<6; n++) { ss >> val; cout << val*2 << endl; } return 0; }
Now I really feel stupid...I have no clue what that bit of code does. Unless I'm totally wrong, that doesn't convert an int variable to a string. Specifically, I have to change the value 135 (or whatever) minutes into "2:15." I know there has to be an easy way to do this. I wish I could just use java...String.valueOf(int x).
Oops, sorry about that, it's doing string to int. So you want int to string?
PHP Code:
#include <stringstream>
#include <iostream>
#include <string>
using namespace std;
string itos(int i) // convert int to string
{
stringstream s;
s << i; // sent i to the stringstream
return s.str();
} // return the contents of the stream
// converted to a string
int main()
{
int i = 127;
string ss = itos(i); // Our function
cout << ss << endl; // Print it
}
Last edited by Eibro; 10-13-2002 at 08:53 PM.
thanks alot...that was what i was looking for.
Code:#include <iostream.h> #include <stdlib.h> int main() { int num = 15; char string[10]; itoa(num, string, 10); return 0; }
Is Stringstream not in VC++?
#include <sstream>Originally posted by Extol
Is Stringstream not in VC++?