2016-08-27 39 views
0

我试图逐字读取char buffer中的double值。在该文件中我有一个像0.032 0.1234 8e-6 4e-3等一些值这里是我的代码,这是使用atof()功能转换成Word(存储在阵列“s”)为双精度值numatof()无法在8e-6中读取'e'

char s[50]; 
double num; 
while(getline(str, line)) 
{ 
    int i=0, j=0; 
    while(line[i] != '\0') 
    { 
     if(line[i] != ' ') 
     { 
      s[j] = line[i]; 
      //cout << s[j]; <-- output of s[j] shows correct values (reading 'e' correctly) 
      j++; 
     } 
     else 
     { 
      num = atof(s); 
      cout << num << " "; 
      j=0; 
     } 
     i++; 
    } 
    cout << endl; 
} 

每当数字,其中包括“E '在他们(如8e-6)来,atof()函数只为他们返回0。以上示例值的输出结果为0.032 0.1234 0 0

我试图检查演示数组,atof()工作正常。

char t[10]; 
    t[0] = '8'; 
    t[1] = 'e'; 
    t[2] = '-'; 
    t[3] = '5'; 
    t[4] = '\0'; 

    double td = atof(t); 
    cout << "td = " << td << endl; 

这里输出td = 8e-05 我既包括<stdio.h><stdlib.h>。任何想法为什么它不工作?

+0

你期待什么答案? –

+0

我期待'0.032 0.1234 8e-06 4e-03'。 – Jaipreet

+0

请学习如何调试。这很简单,是你现在需要的最重要的技能。 – Amit

回答

0

你必须传递到功能atof()只有double正确representaton,否则返回值为0。

如果您将检查非常简单的代码 字符P [] =“6E-5”; double a = atof(p); 你会看到它正在工作。

这意味着你有一个问题,而不是6e-5号码是自己的,但错误地表示你的字符串s