【发布时间】:2018-08-07 07:53:33
【问题描述】:
如何将字符串转换为浮点数?
这是我的代码:
string A[3] = {"20","21"};
float convertA ;
convertA = atof (A[1]) ;
cout << convertA << endl ;
这就是显示的内容:
-1 #lND
关于我可能做错的任何建议?
【问题讨论】:
-
它甚至可以编译吗?那些字符串是
std::strings 吗? -
这不会编译。你想要:
convertA = atof(A[1].c_str());,除非string不是std::string;我不能只用 4 行代码来判断。 -
在
std::string变量上使用std::stof。atof用于 C 字符串,即const char * -
输出仍在发出-1。 #IND
-
这不是您的完整代码。请发帖minimal reproducible example。
标签: c++ visual-c++ visual-studio-2012