在c ++中从一个字符到另一个字符从字符串传递到int时的不精确度

问题描述 投票:0回答:1

从字符串'99'(或小于100的任何其他数字)传递到int 99时没有问题。但是,当我尝试大于99的数字时,它给出了(数字-1)。我也发现这种情况发生在数字小于1000的情况下,但是从1000到10000没关系,但是我测试了大于10 ^ 4的数字,这又给了(数字-1)。这是我的转换字符串的代码

//Search how many nums are in the string wer are passing until an space or new line

int nums = 0;

for(int j = i; j < s.size(); j++){
  if(s[j] == ' ' || s[j] == '\n') break;
  nums++;
 }

 //pass to the variable time the string character by character
 int time = 0;

 while(nums--){
    time += (s[i] - '0') * (pow(10, nums));
    i++;
}

我想知道我的计算机是否有错误或我丢失了什么。

c++ type-conversion int character
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.