Cast int to string

While trying to figure out how to cast an int to a string I came across this code:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
 
int main() 
{
    double f = 23.43;
    std::string f_str = std::to_string(f);
    std::cout << f_str << '\n';
}


Unfortunately, using Dev C++ Version 5.4.2, I'm getting an error:

[Error] 'to_string is not a member of 'std'

Any ideas how to fix this?

Thanks in advance.
Last edited on
1. You don't have any ints here.
2. You don't need to cast it to a string if you are just using std::cout
3. std::to_string is C++11, Many compilers still don't support it. DevC++ probably never will.

here are some solutions:
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    int i = 43;
    std::cout << i;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>

int main()
{
    int i = 43;
    std::stringstream ss;
    ss << i;

    std::string str;
    ss >> str;

    std::cout << str;
}
Last edited on
Whoops! That was a copy and paste from site where I found the code--I forgot to change the double to int.

Anyway, thanks for the alternate method.
Topic archived. No new replies allowed.