2013-02-12 80 views
1

我很难看出为什么我的代码不起作用。出现问题的部分是当我将文本文件中的值传递给我的等级变量时。我不明白为什么这是错的。无论如何,我的代码的其他一切都很好,但我还是把它包括在内了。从文本文件读入矢量

string fileName; 

cout << "Program that takes data from file and calculate\nmean and standard deviation and put it in file out.txt\n"; 
cout << "Enter the input file name "; 
cin >> fileName; 
    double grade; 
ifstream inData; 

inData.open(fileName.c_str()); 

// Declare vector<double> vecx 
vector<double> vecx; 
// read data from input file to vector vecx, 
while(inData >> grade) 
{ 
    vecx.push_back(grade); 
} 


inData.close(); 
// keep track how many elements you read from file 
// When you done with reading from file, close stream inData 

这里是完整的代码,如果你有兴趣。

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <cmath> 

using namespace std; 

int main() 
{ 
    string fileName; 

    cout << "Program that takes data from file and calculate\nmean and standard deviation and put it in file out.txt\n"; 
    cout << "Enter the input file name "; 
    cin >> fileName; 
     double grade; 
    ifstream inData; 

    inData.open(fileName.c_str()); 

    // Declare vector<double> vecx 
    vector<double> vecx; 
    // read data from input file to vector vecx, 
    while(inData >> grade) 
    { 
     vecx.push_back(grade); 
    } 


    inData.close(); 
    // keep track how many elements you read from file 
    // When you done with reading from file, close stream inData 

    // read element by element in vector vecx and calculate sum; 
    double sum=0; 
    double average; 
    for (int i=0; i < vecx.size(); i++) 
     sum=sum+vecx[i]; 

    average=sum/(vecx.size()); 
    // sum divide by number of elements to find mean(average) 

    //again read element by element and calculate 
    //square of difference between element and mean 
    //calculate sum of squares of differences 
    //divide by number of elements and 
    //take square root - you got the standard deviation 

    sum=0; 
    double variance, stdev; 
    for (int i=0; i < vecx.size(); i++) 
     sum=sum+(vecx[i]-average*vecx[i]-average); 

    variance=sum/(vecx.size()); 
    stdev=sqrt(variance); 

    //open output stream 
    ofstream outData; 
    outData.open("out.txt"); 
    //output mean and standard deviation 

    cout << "Average is " << average << endl; 
    cout << "Standard deviation is " << stdev << endl; 

    //close stream 
    outData.close(); 
} 

我感觉自己就像没有看到为什么这是不工作...我应该能想出解决办法,但我不是白痴。

+2

如果您发布了您正在获取的错误,而不是简单地“它不起作用”,这将会很有帮助。 – SShaheen 2013-02-12 16:48:54

+0

究竟是什么问题?读完之后'vecx'是空的吗? – David 2013-02-12 16:48:58

+2

没有看到数据文件(它必须是以空格分隔的**文本**表示双值),*和*知道文件已成功打开(在当前源中永远不会验证),因此无法回答这准确。你能详细说明“它不工作”的*确切的意思吗? – WhozCraig 2013-02-12 16:50:35

回答

9

C++遵循先例的正常数学顺序。

这条线:

sum=sum+(vecx[i]-average*vecx[i]-average); 

看起来不正确。乘法,所以它计算这个减法之前发生:

sum=sum+((vecx[i]-(average*vecx[i]))-average); 

但想必你的意思是这样的:

sum=sum+((vecx[i]-average)*(vecx[i]-average)); 

你可能有兴趣看到the full list of precedence ordering

+0

+1这肯定是一个**问题,并且很可能** **问题,假设数据文件被正确读取。尖锐的眼睛,先生。 – WhozCraig 2013-02-12 16:55:16

+0

虽然看到SShaheen的答案也许它是一些事情 – 2013-02-12 16:57:22

+0

很难说,不知道究竟是什么问题@Glassjawed有。 – SShaheen 2013-02-12 16:58:39

5

您从不将数据写入您的文件。使用您的ofstream outData而不是std::cout。否则,你只是将你的输出发送到控制台。

//open output stream 
ofstream outData; 
outData.open("out.txt"); 
//output mean and standard deviation 

outData << "Average is " << average << endl; 
outData << "Standard deviation is " << stdev << endl; 

//close stream 
outData.close(); 
+0

是的,这是问题。我没有得到任何东西传入我的载体,我不知道为什么。我应该早些时候更清楚。 – Glassjawed 2013-02-12 17:10:47

+0

**编辑**是的,这是一个问题。然而,我遇到的主要问题是,我没有得到任何传入我的矢量的东西,我不知道为什么。我应该早些时候更清楚。 现在它将打印NaN作为我的平均值和stdev值。 – Glassjawed 2013-02-12 17:16:57

+0

查看ifstream的参考资料:http://www.cplusplus.com/reference/fstream/ifstream/。您可能会发现它无法找到您提供的文件,但可以测试它是否“错误” - 即无法打开文件。 – 2013-02-12 17:18:31