Return size of array to main

Hello!

I am currently learning c++, in my second week now, and I am stuck on a assignment.

I have a vector in it's own function:
int VaderStationer(int index)
{
vector<int> VaderStationer(index);
for (int i = 0; i < index; i++)
{
int dayX = 1;
int measurement;
cout << "Type in the measurements for day " << dayX << ": ";
cin >> measurement;
dayX++;
}
return index;
}

What I want is to return the size of the vector to another function, where it will print out the data contained in the vector through a for loop, though I do not understand how to code it.

If the vector would be in the same function as where it should be printed, it would be no problem to solve for me, but I do not understand how I can send the size of the vector from the vector function to the print function.

I tried with a normal for loop, except that I don't know what to write for arguments there. for (int i = 0; i < "size of vector"; i++).

Someone who could lend me a hand?
From my understanding of the problem you could return the vector.

Then you could pass that vector into a function and then use the size member to loop through each index.

If so, then I think something like this is what you are looking for.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Example program
#include <iostream>
#include <string>
#include <vector>

using namespace std;

//return a vector that contains the information
vector<int> VaderStationer(int index) 
{
    vector<int> VaderStationer(index);
    int dayX = 1;

    for (int i = 0; i < index; i++)
    {
        int measurement;
        cout << "Type in the measurements for day " << dayX << ": ";
        cin >> measurement;
        VaderStationer[i] = measurement;
        dayX++;
    }
    //return the information
    return VaderStationer;
}

void print(const vector<int>& items) {
    //use the size member to loop through the vector
    for (size_t i = 0; i != items.size(); ++i) {
        cout << items[i] << ' '; //display each index of the vector
    }
}

int main()
{
    size_t size;
    cin >> size;
    vector<int> vaderStationer = VaderStationer(size);
    // pass the vector into the function
    print(vaderStationer);
}


Thank you very much. This is indeed what I was looking for, noticed also that I missed a couple of things like VaderStationer[i] = measurements;

Now I just gotta figure out why I doesn't print out the data I enter and learn the code you added.
Topic archived. No new replies allowed.