3

I am having an issue trying to convert a number into a string. The purpose is for error checking to make sure the number is of a specific length. I have tried using both to_string() and convert.str() functions but get the same error back when trying to compile.

I am using MinGw g++ to compile and realize I need to tell it I want the C++11 standard, which I believe I have done. My compiler code is as follows:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe

Now assuming that is correct, my code for using to_string() is as follows:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
  int book_code = 0;

  cout << "Please enter the four digit book code: ";
  cin >> book_code;
  string code = to_string(book_code);

  while (!(cin >> book_code) || code.length() != 4){
    cin.clear();
    cin.ignore(10000, '\n');
    cout << "That is not a valid code." << endl;
    cout << "Please enter the four digit book code: ";
  }
} 

And my code for using convert.str() is as follows:

int main() {
  int book_code = 0;

  cout << "Please enter the four digit book code: ";
  cin >> book_code;
  ostringstream Convert;
  convert << book_code;
  string code = convert.str();

  while (!(cin >> book_code) || code.length() != 4){
    cin.clear();
    cin.ignore(10000, '\n');
    cout << "That is not a valid code." << endl;
    cout << "Please enter the four digit book code: ";
  }
} 

Neither of these was successful and both returned

error: 'to_string' was not declared in this scope

Am I missing something obvious?

Newd
  • 2,174
  • 2
  • 17
  • 31
Grr
  • 15,553
  • 7
  • 65
  • 85
  • 2
    I bet that this is a duplicate of this question : http://stackoverflow.com/questions/15569179/to-string-not-declared-in-scope?rq=1 – BЈовић Jun 29 '15 at 14:03
  • Seems to compile (and run); http://coliru.stacked-crooked.com/a/01ee396b5116e4a3 – Niall Jun 29 '15 at 14:31
  • I'm 90% sure it's an issue with old versions of mingw. Try mingw-w64, that project actually gets updated (and despite the name supports 32 bits binaries) – KABoissonneault Jun 29 '15 at 14:58
  • possible duplicate of [to\_string is not a member of std, says so g++](http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g) – Jean Pierre Dudey Jul 01 '15 at 14:17
  • Jean, I followed the instructions in that post and unfortunately I received and error: First it says g++.exe has stopped responding. I click cancel and then i get an application eror. "The application was unable to start correctly (0xc0000005). – Grr Jul 01 '15 at 16:43

2 Answers2

11

In MinGW std::to_string() does not exist, you should declare your own implementation.

std::string to_string(int i)
{
    std::stringstream ss;
    ss << i;
    return ss.str();
}

I recommend you to use MSYS2, it is more actualizated and you can avoid this type of problems.

Edit:

Checking the dot position in double:

#include <iostream>
#include <sstream>
#include <string>

std::string to_str_with_dot_pos(double i, unsigned int &pos)
{
    std::stringstream ss;
    ss << i;

    std::string result(ss.str());

    pos = 0;
    while (pos < result.length() && result[pos] != '.') {
        pos += 1;
    }

    return result;
}

int main(int argc, char **argv)
{
    double d(12.54);
    unsigned int pos(0);

    // str should be "12.54".
    // pos should be 2.
    std::string str = to_str_with_dot_pos(d, pos);
    std::cout << "double as string: " << str << std::endl;
    std::cout << "double dot position: " << pos << std::endl;

    return 0;
}

Explanation of the code (the while loop):

It gets every character of the std::string and checks if it does not equals to the . dot character, if the character is not equal to . it will add +1 to the pos variable.

It returns 2 and not 3 because we're counting from 0, not 1.

Also, this question is a duplicate.

Community
  • 1
  • 1
  • Jean. Your suggestion works nicely, but I have a follow up for you. Say I wanted to convert a double to a string i.e. 12.54 and then check for the location of the '.' character. Obviously I would change int i to double i. But searching for '.' with find() returns a very large and arbitrary number. Any suggestions? – Grr Jul 09 '15 at 18:16
  • I updated the answer, check it! I don't know if that code is what you're looking for, but it works :p – Jean Pierre Dudey Jul 09 '15 at 19:13
  • you don't need to always use `std::` if you add `using namespace std;` below the `#include`s – FluorescentGreen5 Jun 30 '16 at 04:47
0

Check that your version of MinGw support to_string, as the code above compiles correctly.

I'd recommend a different approach for length checking, one that avoids using strings:

#include <iostream>
#include <cmath>

using namespace std;

int is_len(int number, int len)
{
    if(pow(10, len-1) <= number && number < pow(10, len))
        return true;

    return false;
}

int main()
{
    int number = 1000;

    cout << is_len(1, 2) << endl;
    cout << is_len(1005, 4) << endl;
    cout << is_len(9999, 4) << endl;
    cout << is_len(599, 4) << endl;
    cout << is_len(1005, 5) << endl;

    return 0;
}

Prints:

0

1

1

0

0

Jaka Konda
  • 1,385
  • 3
  • 13
  • 35