Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating and implementing functions (C++).
#9
All right, fair enough. I've tried passing by reference and it just created a whole mess for me, so I just went with declaring within functions. o_e I'll have to devote some time to understanding how to pass by reference.

Now I've run into another problem.

Code:
float SumOfSquares (istream &inF)
{
    //Start from the beginning of the file - Rewind.
    inF.clear();
    inF.seekg(0L, ios::beg);

    float sqValue = 0;
    float sqSum = 0;
    float value;

    while (inF >> value)    
    {
        sqValue = value * value;
        sqSum += sqValue;    //Add sqValue to sqSum
    }
    return sqValue;
}

This is giving me 36 as an end result (since 6 is the last number and there are 7 values in my test file), meaning that it's acting as if sqSum is always 0, and that's been declared outside of the loop. I'm not sure what I could have done wrong here.
Reply


Messages In This Thread
Creating and implementing functions (C++). - by MariettaRC - 2013-09-16, 01:31 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)