Passing variables between functions in C++
<p>My task is to type and read() several double variables and store them in vector, then the function compute() should calculate the sum of all variables stored in the vector and their average. I have fixed these functions and they work well. The problem is with the final function print() .. the function should print out the result - 'sum' and 'average' variables. But my code for print() is not working properly and prints out wrong numbers. Here is my code: </p>

<pre><code>#include &lt;iomanip&gt;
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt; 
using namespace std;

vector&lt;double&gt; read(){//this function works fine
    cout &lt;&lt; "Write some numbers with space between them?" &lt;&lt; endl;
    double numz;
    vector&lt;double&gt; myvector;
    do {
    cin &gt;&gt; numz;
    myvector.push_back (numz);
    } while (numz);
    return myvector;
}  

void compute(double average, double sum, vector&lt;double&gt; &amp;myvector){//this function works fine
//if i put cout in this function it calculates correctly
    vector&lt;double&gt;::iterator it;
    for ( it=myvector.begin() ; it &lt; myvector.end()-1; it++ ){
    sum += *it;
    }
    average = sum/myvector.size();

}

void print(double average, double sum){//this is printing out wrong numbers
    cout.precision(2); 
    cout &lt;&lt; sum &lt;&lt; endl;
    cout &lt;&lt; fixed  &lt;&lt; average &lt;&lt; endl;
}


int main (){
    vector&lt;double&gt; myvector = read();
    double average;
    double sum;
    compute(average, sum, myvector);
    print(average, sum);//this is printing out wrong numbers

    system("pause");
  return 0;
}
</code></pre>

<p>Thanks in advance for any help, i understand that my mystake is in variable passing between functions but i have spent the whole day debugging and reading tutorials without any luck. </p>
