EmilDo
EmilDo

Reputation: 1177

Passing variables between functions in C++

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:

#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <vector> 
using namespace std;

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

void compute(double average, double sum, vector<double> &myvector){//this function works fine
//if i put cout in this function it calculates correctly
    vector<double>::iterator it;
    for ( it=myvector.begin() ; it < 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 << sum << endl;
    cout << fixed  << average << endl;
}


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

    system("pause");
  return 0;
}

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.

Upvotes: 1

Views: 14311

Answers (3)

Zhi Wang
Zhi Wang

Reputation: 1168

There are several flaws in your program:

  1. The declaration of compute maybe better to changed to:

    void compute(const vector<double> &myvector, double &average, double &sum);
    

    Passing a const vector<double>& makes it not modifiable in the function, and takes the references of average/sum makes changing the passing in arguments possible, see more of "formal argument" and "actual argument" here.

  2. You'd better declares function read as:

    void read(vector<double>& myVector);
    

    as this will avoid copying of the vector when function read returns.

Upvotes: 0

Johannes Thoma
Johannes Thoma

Reputation: 1076

You forgot the reference operator (&) in the declaration of the compute function: Without it the variables in the main() function will not be modified: Write

void compute(double &average, double &sum, vector<double> &myvector){//this function works fine

instead of what you wrote and it should work.

Upvotes: 0

emartel
emartel

Reputation: 7773

Just add & in front of your variable in the function declaration and it will pass a reference to your variable!

void compute(double& average, double& sum, vector<double> &myvector)

Upvotes: 5

Related Questions