c++ - 变量似乎在自行改变值(value)

标签 c++ variables

好吧,我的项目是一个分析 .txt 文件的程序,该文件包含一串不同长度的 DNA 链。我得到了 3 个函数的全部工作,但我的老师希望我们进行 oo 编程。所以我把我的代码放在一个类中并将它分解成不同的函数。但是现在,我的变量似乎随机改变了它们的值,我不知道为什么。

我用我的“sum”变量运行了一堆测试(但它不是唯一这样做的)并且它在函数中计算出正确的值但是如果我在我的主函数中计算出“sum”的值,该值被更改为一个荒谬的数字。

这是代码:问题变量在哪里以及如何使用并不是我的整个程序。 如果这些代码不足以显示问题,我可以添加更多我只是不想让它变得困惑。

DNA处理.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

最佳答案

由于 sum 被声明为 double 值,它的值 0 可能不会完全存储为零,出于所有实际目的,3.18337e-314 的值可以被视为零。你可以定义一个阈值

double epsilon = 0.00001 ; // depending on precision

如果 sum < epsilon,则 sum = 0.0(虽然不需要) 在您的示例中,您也使用了 sum 作为局部变量,要么不声明局部变量,只使用成员变量,要么将局部变量声明为不同的名称以避免混淆

关于c++ - 变量似乎在自行改变值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25780404/

相关文章:

c# - 错误 - 必须声明标量变量 - C#

javascript - jQuery - 在函数之间共享变量

javascript - 为什么 jQuery 选择器变量不能在命名空间内工作?

c++ - 我的 'Person' 类有什么问题?

android - 循环初始声明只允许在 C99 或 C11 模式下使用

c++ - g++在头文件中包含目录

c++ - 在存在异常的情况下使用 for_each? std::exception_list

java - 难度设置变量

python - python程序中使用的变量列表

c++ - getrusage 没有像我预期的那样工作