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++).
// Example program
#include <iostream>
#include <string>
#include <vector>
usingnamespace 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);
}