C++ C++;带有不需要的字符的数据文件:如何删除恶意字符并计算平均值、标准偏差和标准误差

C++ C++;带有不需要的字符的数据文件:如何删除恶意字符并计算平均值、标准偏差和标准误差,c++,C++,我有一个包含49个数字+1个单词的数据文件和另一个包含50个数字的文件。我的任务是计算两者的平均值、标准差和标准误差。目前,我有一个代码,它将愉快地为只包含数字的文件计算正确的值。如何删除该角色 我是一个初学者,不知道如何正确地使用getline()函数将文件中的数据放入字符串,然后以某种方式使用cin.ignore()和cin.clear()删除字符?谢谢你的帮助 节目: // Assignment 2: Milikan data programme #include "stdafx.h" #

我有一个包含49个数字+1个单词的数据文件和另一个包含50个数字的文件。我的任务是计算两者的平均值、标准差和标准误差。目前,我有一个代码,它将愉快地为只包含数字的文件计算正确的值。如何删除该角色

我是一个初学者,不知道如何正确地使用getline()函数将文件中的数据放入字符串,然后以某种方式使用cin.ignore()和cin.clear()删除字符?谢谢你的帮助

节目:

// Assignment 2: Milikan data programme
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cmath>
#include<string>


using namespace std;
//Mean function
double mean(double *mydata, double N)
{ 
    double sum(0), m;
    for (int i=0; i<N; i++)
    {
        sum += mydata[i];
    }
    m = (sum/(double)N);
    return(m);
}
//Standard deviation function
double standard_dev(double *mydata, double m, int N)
{
    double *mydata2 = new double[N];
    for (int i=0; i<N; i++)
    {
        mydata2[i] = pow((mydata[i]-m), 2);
    }
    double sum(0), S, X;
    for (int i=0; i<N; i++)
    {
        sum += mydata2[i];
    }
    X = sum/(N-1);
    S = sqrt(X);
    return (S);
}

int main ()
{
    int N(0);
    char filename[100];
    double m, sterr, stdev;
    string temp;

    cout<<"Please enter the number of data points:  ";
    cin>> N;
    cout<<"Please enter the name of the file:  ";
    cin>>filename;

    //Dynamic memory allocation for N data points in array mydata
    double *mydata;
    mydata = new double[N];

    //Open file and attach chosen file to myfile
    ifstream myfile;
    myfile.open(filename);

    //Check it opened sucessfully 
    if(!myfile.is_open())
    {
        cerr<<"\nError: file could not be opened!"<<endl;
        system("PAUSE");
        return(1);
    }

    //Detect and ignore rogue character???


    //Read data from the file into an array
    for (int i=0; i<N; i++)
    {
        myfile>>mydata[i];          
    }


    m = mean(mydata, N);                                                                   
    stdev = standard_dev(mydata, m, N); 
    sterr = 1/stdev;

    cout<<"\nThe mean charge of an electron is  : "<<m<<" eV"<<endl; /
    cout<<"The standard deviation of results is : "<<stdev<<endl;
    cout<<"The standard error of results is : "<<sterr<<endl;

    myfile.close();     //Close file
    delete[] mydata;    // Free memory
    system("PAUSE");
    return 0;
}
//作业2:Milikan数据程序
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//平均函数
双平均值(双*我的数据,双N)
{ 
双和(0),m;

对于(int i=0;i还请记住,如果数字提取失败,流对象
failbit
将被设置。您可以对此进行检查,如果设置,则跳过流中的所有非数字字符,直到再次看到数字为止

类似于以下伪代码:

while (myfile)
{
    myfile >> value;

    if (myfile.fail())
    {
        clear_failbit();

        while (next_character_is_not_digit())
            get_and_discard_next_character();
    }
}

当然,更好的解决方案可能是不生成包含错误的文件。

每个数字都在单独的一行吗?不需要的字符或字符串也在单独的一行吗?请不要将文件放在注释中,而是将其摘录放在问题中。1.64638错误1.55023 1.54612 1.54004等。。。。