How to call vectors from function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
using namespace std;
vector<float> avesum(float d)
{
	vector<float> v;
	cout << "please enter numbers: ";
	while (cin >> d) {

		v.push_back(d);
	}
	return v;
}
void printvec(vector<float> v) {

	for (unsigned int i = 0; i < v.size(); ++i)
		cout << v.at(i) << " " << endl;

}
int main() {

	vector<float> avesum(-20.0); /* too call avesum function If I leave empty it says error..so I put random numbers */
	void printvec();

	return 0;
}


I am trying to read values in through function and
also print that vector using a function.
can someone help me out here??
this code doesnt do anything.
Last edited on
Hello redstorm98,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

The two lines in main are proto types not function calls. To be a function call yu do not need the return type. If the function does return something it needs to return to a variable.

Main should look more lie this:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    std::vector aName;

    aName = avgsum(20.0);

    print (aName);

    //   May need something here to pause the program before ending.
    return 0;
}


It has been my experience that passing a vector to a function by reference makes things much easier.

Hope that helps,

Andy
Topic archived. No new replies allowed.