Hasselhaub
Hasselhaub

Reputation: 41

Variables seem to be changing value on their own

Ok so my project is a program that analyses a .txt file that has a bunch of DNA strands of varying lengths. I got it all to work in 3 functions but my teacher wants us to us oo programming. So i put my code in a class and broke it up into different functions. Now, however my variables seem to randomly change their value and I don't know why.

I ran a bunch of tests with my "sum" variable (but it is not the only one doing this) and it calculates the correct value in the function but if I cout the value of "sum" back in my main, the value is changed to a ridiculous number.

Here is the code: it is not my whole program just where the problem variable is and how it is used. If this isnt enough code to show the problem i can add more i just didnt want to make it cluttered.

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{}

    }

}

output while passing my test .txt file into this program.

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

Upvotes: 2

Views: 1713

Answers (2)

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

Since sum is declared as double, it's value of 0 may not be stored exactly as zero, for all practical purposes, value of 3.18337e-314 can be considered as zero. You may define a threshold value

double epsilon = 0.00001 ; // depending on precision

and if sum < epsilon, sum = 0.0 (not needed though) In your example, you have used sum as a local variable as well, either don't declare local variable, just use the member variable or declare the local variable as different name to avoid confusions

Upvotes: 1

Harish Vats
Harish Vats

Reputation: 662

The value of a local variable is valid within the scope of the function,thats why you are getting correct answer inside method. But no value is returned back,therefore garbage value is printed in the main.

Try sending the variable in the method by reference, then their exact value will be available in main also. Try it.

Upvotes: 0

Related Questions