变量似乎在改变自己的价值

时间:2014-09-11 06:19:39

标签: c++ variables

好的,所以我的项目是一个分析.txt文件的程序,该文件包含一堆不同长度的DNA链。我把它全部用于3个功能,但我的老师希望我们在编程中使用。所以我将我的代码放在一个类中,并将其分解为不同的函数。现在,我的变量似乎随机改变了它们的价值,我不知道为什么。

我用我的"总和"进行了一系列测试。变量(但它不是唯一一个这样做的)并且它计算函数中的正确值但是如果我输出&#34的值;总和"回到我的主要部分,价值变成了荒谬的数字。

这是代码:问题变量是什么以及如何使用它不是我的整个程序。 如果这个代码不足以显示问题我可以添加更多我只是不想让它变得杂乱。

DNAProcessing.cpp

void DNAProcessing::CalcSumAndMean()
{
    int lineLength = 0;
    int lineCounter = 0;
    int wholeFileStringLen = 0;
    double sum = 0;
    double mean = 0;
    string wholeFileString = "";
    string line;
    bool filefail = false;



    ifstream DNAFile;

    DNAFile.open(nameoffile.c_str());



    if(DNAFile.fail())

    {
        filefail = true;

        return;

    }
    else
    {
        cout << "\nYour data was processed\n" << endl;
    }



    while(DNAFile >> line)

    {

        //cout << line << endl;

        lineCounter += 1;

        lineLength = line.length();

        sum += lineLength;



        wholeFileString += line;        

    }

    cout << "sum: " << sum << endl;  // with my test .txt file this outputs 736

    mean = (sum / lineCounter);

    wholeFileStringLen = wholeFileString.length();
    cout << "sum: " << sum << endl; // with my test .txt file this outputs 736
}

的main.cpp

int main()

{

    srand(time(0));


    bool noexit = true;
    string yesorno;
    string filename;


    while(noexit == true)

    {

        cout << "Would you like to process a list of DNA strings? (y/n)" << endl;

        cin >> yesorno;



        if((yesorno == "y") || (yesorno == "Y" ))

        {

            cout << "please input the name of the file you wish to process." << endl;

            cin >> filename;



            DNAProcessing DNAStrandFile(filename);
            DNAStrandFile.CalcSumAndMean();
            cout << "sum: " << DNAStrandFile.sum << endl; //for some reason sum turns into 3.18337e-314 and i have no clue why

            if (DNAStrandFile.filefail == false)
            {
                cout << "sum: " << DNAStrandFile.sum << endl; // same here
                DNAStrandFile.CalcNucleobaseRelProb();
                DNAStrandFile.CalcBigramRelProb();
                DNAStrandFile.CalcVarianceAndStndDev();
                DNAStrandFile.CalcNormRand();
                DNAStrandFile.PrintData();
                DNAStrandFile.PrintNewList();

            }
            else
            {
                cerr << "No file found" << endl;
            }




        } 

        else if((yesorno == "n") || (yesorno == "N"))

        {

            noexit = false;

        } 

        else{}

    }

}
将测试.txt文件传递给此程序时输出

sum: 736
sum: 736
sum: 3.18337e-314
sum: 3.18337e-314

2 个答案:

答案 0 :(得分:1)

由于sum被声明为double,因此它的值0可能不会精确地存储为零,出于所有实际目的,3.18337e-314的值可以被视为零。您可以定义阈值

double epsilon = 0.00001 ; // depending on precision

如果总和&lt; epsilon,sum = 0.0(虽然不需要) 在您的示例中,您还使用了sum作为局部变量,或者不声明局部变量,只使用成员变量或将局部变量声明为不同的名称以避免混淆

答案 1 :(得分:0)

局部变量的值在函数范围内有效,这就是为什么你在方法中得到正确答案的原因。 但是没有返回任何值,因此垃圾值将打印在主页中。

尝试通过引用在方法中发送变量,然后它们的确切值也将在main中可用。试试吧。

相关问题