Itoa (integer & String)

Hello,

I am aware that there are other subjects on this mater but they all involve "char" and what not.

What I really need is for someone to help me to get the data from an "int" to a "string".

The code of interest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  

// the "..." are parts of the code irrelevant to the problem
//...
        int number;
	string Minus;
	string Number;
//...

cin>> number;

//...

Number = (int)abs(number); // this is where the screw up occurs

//...



Okay so there it is. I know I need to convert it somehow and that the last line of code there is wrong.

I'd be grateful for any help.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int num = 5;
    stringstream ss;
    ss << num;
    string str = ss.str();
    return 0;
}


You could put it in a small function if you wanted to.

1
2
3
4
5
6
7
template <class T>
string to_string(T &t)
{
    stringstream ss;
    ss << t;
    return ss.str();
}


Have a look at this:
http://www.cplusplus.com/reference/iostream/stringstream/str/
Thanks allot!
Topic archived. No new replies allowed.