To_string and convert.str () are not declared in scope
I am having a problem converting a number to a string. The goal is error checking to make sure the number is of a certain length. I tried using functions to_string()
and convert.str()
, but I get the same error when trying to compile.
I am using MinGw g ++ for compilation and implementation. I need to say that I need the C ++ 11 standard , which I believe I have done. My compiler code looks like this:
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 this is correct, my code to use to_string()
looks like this:
#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 () looks like this:
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: ";
}
}
None of them were successful and both returned
error: 'to_string' was not declared in this scope
Am I missing something obvious?
source to share
std::to_string()
Doesn't exist in MinGW , you have to declare your own implementation.
std::string to_string(int i)
{
std::stringstream ss;
ss << i;
return ss.str();
}
I recommend that you use MSYS2 , it is more up to date and you can avoid problems like this.
Edit:
Checking the position of a point 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 (loop while
):
It gets each character std::string
and checks if it matches the .
dot character , if the character is not equal .
, it will add +1 to the variable pos
.
It returns 2, not 3, because we are counting from 0, not 1.
Also, this question is a duplicate .
source to share
Make sure your MinGw version supports to_string as the code above compiles correctly.
I would recommend a different approach for checking the length, which 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;
}
Printing
0
1
1
0
0
source to share