从文本文件读入矢量

时间:2013-02-12 16:45:38

标签: c++

我很难理解为什么我的代码不起作用。问题的部分是当我将文本文件中的值传递到我的成绩变量时。我不明白为什么这是错的。我的代码中的其他所有内容都很好,除此之外,但无论如何我都把它包括在内。

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();