简体   繁体   English

C ++ atoi()仅返回第一个数字

[英]C++ atoi() only returns the first digit

I am experiencing a strange problem with atoi() 我在使用atoi()时遇到了一个奇怪的问题

I am doing a conversion like this: 我正在做这样的转换:

cout<<atoi(mystring.c_str())<<endl;

mystring is a string with something like 3245524 mystring是一个类似3245524的字符串

The above code would only return 3 to the stdout. 上面的代码只会将3返回到标准输出。

Now, if I make the string smaller, eg less than 1000000, then I get the entire number returned to stdout. 现在,如果我使字符串变小,例如小于1000000,那么我会将整个数字返回到stdout。

Any ideas what is causing this problem? 有什么想法导致此问题吗? This is still well below the limit of C++ int so it is not some overflow. 这仍然远远低于C ++ int的限制,因此不会发生溢出。

EDIT, some additional information. 编辑,一些其他信息。 When I simplify the command to simply: 当我将命令简化为:

cout<<mystring.c_str()<<endl;

The stdout is something like 3.24552e+06 标准输出类似于3.24552e + 06

Is the problem related to this? 问题与此有关吗?

From your edit: 根据您的编辑:
The contents of mystring are "3.24552e+06" , ie a scientific (floating-point) string representation for the number 3245520. mystring的内容为“ 3.24552e + 06”,即数字3245520的科学(浮点)字符串表示形式。
Therefore, atoi parses the first integer it finds in mystring, as expected, which is: 3. 因此,atoi会按预期解析在mystring中找到的第一个整数,即:3。
If mystring contained "3245520", your atoi call would return the integer 3245520. 如果mystring包含“ 3245520”,则atoi调用将返回整数3245520。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM