C++ to_string和convert.str()未在作用域中声明

C++ to_string和convert.str()未在作用域中声明,c++,c++11,tostring,C++,C++11,Tostring,我在尝试将数字转换为字符串时遇到问题。其目的是进行错误检查,以确保数字具有特定长度。我尝试过使用to_string()和convert.str()函数,但在尝试编译时返回了相同的错误 我正在使用MinGw g++编译并实现我需要告诉它我想要C++11标准,我相信我已经做到了。我的编译代码如下: NPP_SAVE CD $(CURRENT_DIRECTORY) C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).e

我在尝试将数字转换为字符串时遇到问题。其目的是进行错误检查,以确保数字具有特定长度。我尝试过使用
to_string()
convert.str()
函数,但在尝试编译时返回了相同的错误

我正在使用MinGw g++编译并实现我需要告诉它我想要C++11标准,我相信我已经做到了。我的编译代码如下:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe
#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: ";
  }
} 
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: ";
  }
} 
现在假设这是正确的,我使用
到_string()
的代码如下:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe
#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: ";
  }
} 
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: ";
  }
} 
#包括
#包括
#包括
使用名称空间std;
int main(){
int book_code=0;
cout>book\u代码;
字符串代码=到字符串(书本代码);
而(!(cin>>图书编码)| code.length()!=4){
cin.clear();
cin.忽略(10000,“\n”);

cout在MinGW
std::to_string()中不存在,您应该声明自己的实现

std::string to_string(int i)
{
    std::stringstream ss;
    ss << i;
    return ss.str();
}
代码解释(while
循环):

它获取
std::string
的每个字符,并检查它是否不等于
点字符,如果字符不等于
它将向
pos
变量添加+1

它返回2而不是3,因为我们从0开始计数,而不是1


此外,这个问题是一个。

检查您的MinGw版本是否支持字符串,因为上面的代码编译正确

我建议使用不同的长度检查方法,避免使用字符串:

#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;
}
#包括
#包括
使用名称空间std;
整数是整数(整数编号,整数长度)
{

如果(pow(10,len-1),我打赌这是这个问题的重复:似乎要编译(并运行);我90%确定这是旧版本的mingw的问题。试试mingw-w64,该项目实际上会得到更新(尽管名称支持32位二进制文件)可能与Jean重复,我按照那篇文章中的说明操作,不幸地收到了错误消息:首先它说g++.exe已停止响应。我单击“取消”,然后我得到一个应用程序eror。“应用程序无法正确启动(0xc0000005).Jean.你的建议很有效,但我有一个后续问题要告诉你。假设我想将double转换为字符串,即12.54,然后检查“.”字符的位置。显然,我会将int I更改为double I。但是使用find()搜索“.”返回一个非常大且任意的数字。有什么建议吗?我更新了答案,检查一下!我不知道该代码是否是您要查找的代码,但它是有效的:如果您在
\include
下添加
使用名称空间std;
,则不必总是使用
std: