-1

I'm working on this code that takes a numeric string and fills an array with each "digit" of the string. The issue I'm having is trying to convert an integer to a string. I tried using to_string to no avail.

Here is the code (note this is pulled from a larger program with other functions):

#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <time.h> 
#include <typeinfo>

    int fillarr(int &length) {
        int arr[length];
        string test = "10010"; //test is an example of a numeric string
        int x = 25 + ( std::rand() % ( 10000 - 100 + 1 ) );

        std::string xstr = std::to_string(x); //unable to resolve identifier to_string
        cout << xstr << endl;
        cout << typeid (xstr).name() << endl; //just used to verify type change

        length = test.length(); //using var test to play with the function
        int size = (int) length;
        for (unsigned int i = 0; i < test.size(); i++) {
            char c = test[i];
            cout << c << endl;
            arr[int(i)] = atoi(&c);

        }
        return *arr;
    }

How can I convert int x to a string? I have this error: unable to resolve identifier to_string.

the_martian
  • 632
  • 1
  • 9
  • 21
  • Possible duplicate of [Easiest way to convert int to string in C++](http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c). You need to explain what went wrong with `std::to_string`, not just say "it didn't work"; we can't psychically debug your issues with the one obvious way to convert `int` to `std::string`. – ShadowRanger Mar 02 '17 at 02:26
  • I know it's similar, but I just don't know why I'm getting an error for what looks like a tried and true method. I'm new to C++ so any tips are more than welcome. – the_martian Mar 02 '17 at 02:28
  • The code won't compile and I left the error in the code as a comment – the_martian Mar 02 '17 at 02:29
  • 4
    `std::to_string` is in which is conspicuously absent from the list of includes. Could be part of your problem. – user4581301 Mar 02 '17 at 02:31
  • Is this your whole program? Do you have a `main` function? – qxz Mar 02 '17 at 02:33
  • I added in the include and the same issue is present. – the_martian Mar 02 '17 at 02:35
  • My main is just a function call to this function with a generic value for 'length' – the_martian Mar 02 '17 at 02:35
  • Obviously a compiler with Variable Length Array support, so not Visual Studio, what Compiler and version? – user4581301 Mar 02 '17 at 02:36
  • 1
    [you shouldn't use `rand()` in C++](http://stackoverflow.com/q/13445688/995714). And [don't use `atoi`](http://stackoverflow.com/q/17710018/995714) – phuclv Mar 02 '17 at 02:36
  • I'm using the latest version of netbeans with c++ – the_martian Mar 02 '17 at 02:37
  • Probably GCC then. Open a command prompt and type g++ -v to get the version. – user4581301 Mar 02 '17 at 02:39
  • 2
    `std::to_string` was added in C++11. C++ 11 isn't turned on by default in any GCC that's likely to ship with Netbeans. Probably 4.8 or 4.9. Anyone know how to turn on C++ 11 support in Netbeans? – user4581301 Mar 02 '17 at 02:40
  • Give this answer a shot: http://stackoverflow.com/a/24591974/4581301 – user4581301 Mar 02 '17 at 02:44
  • Hmm no luck so far. I'll let you know if I come up with a solution. Also thanks for the tips on some the other problems in my code. – the_martian Mar 02 '17 at 02:50
  • also try this one: http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g – M.M Mar 02 '17 at 03:41

2 Answers2

3

As mentioned by user 4581301, you need an #include <string> to use string functions.

The following, though is wrong:

arr[int(i)] = atoi(&c);

The atoi() will possibly crash because c by itself is not a string and that mean there will be no null terminator.

You would have to use a buffer of 2 characters and make sure the second one is '\0'. Something like that:

char buf[2];
buf[1] = '\0';
for(...)
{
   buf[0] = test[i];
   ...
}

That being said, if your string is decimal (which is what std::to_string() generates) then you do not need atoi(). Instead you can calculate the digit value using a subtraction (much faster):

    arr[int(i)] = c - '0';
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
0

Okay I modified my code a bit per suggestion from everyone and ended up handling the conversion like this:

string String = static_cast<ostringstream*>( &(ostringstream() << x) )->str();
cout << String << endl;
the_martian
  • 632
  • 1
  • 9
  • 21