Is have any code can substitute to_string?

The thing is this, I am finishing my homework but my compiler cannot use to_string。I tried using std :: to run it, but it has no effect.Is have any code substitute to_string?

This is my code

string data2string(int data[], int size, int active_index)
{

string s = "[";
for(int i=0; i<size; ++i)
{

if(i==active_index)
s += "active="+ std::to_string(data[i])+", ";
else
s += std::to_string(data[i])+", ";
}
s += "]";
return s;

}

my compiler cannot use to_string

What is your compiler? std::to_string requires C++11 or later to be available.

Please don't say Turbo C or some other geriatric and antiquated compiler.
I use Code block 20.03, my teacher does not allow the use of other compilers.This is also a very funny thing. I followed my textbook, but the teacher ’s compiler does not support it.
Last edited on
Do you actually have
#include <string>
in your code?
yes,i have string and string.h
> my teacher does not allow the use of other compilers.
Which compiler do they mandate you use then?

I Use Code::Block
Yes, we know you use code blocks.

What does you tutor use?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

#include <string.h>
#include <sstream>

using namespace std;

string IntToString(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}

string data2string(int data[], int size, int active_index)
{
    
    string s = "[";
    for(int i=0; i<size; ++i)
    {
        
        if(i == active_index)
            s += "active="+ IntToString(data[i]) /*std::to_string(data[i])*/ +", ";
        else
            s += IntToString(data[i]) /*std::to_string(data[i])*/ + ", ";
    }
    s += "]";
    return s;
}

int main()
{
    int d[]{11,2355,35,47,59,611};
    
    int size = sizeof(d)/sizeof(int);
    cout << "size = " << size << '\n';
    
    string result = data2string(d, size, size);
    
    cout << result << '\n';
    return 0;
}


size = 6
[11, 2355, 35, 47, 59, 611, ]
Program ended with exit code: 0


The additional function above is one way of going around the (well-known) problem of CodeBlocks and it's associated compilers that are out of date - all the more reason not to use it.

Google the problem if you want to go down the rabbit hole of getting a compiler that works with CodeBlocks.

PS I stole the IntToString function from https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
Last edited on
The problem has been solved, thank everybody for helping me
Topic archived. No new replies allowed.