Returning vector from function

Hello, I tried making my first function returning a vector, but I am having some problems with it. Is this set up correctly, with the function initialization and return statement?

1
2
3
4
5
6
7
8
9
10
11
vector<double> calculateSeries(int lån, int år) {
    constexpr int rente = 10;
    vector<double> innbet(år);
    double remainderLoan = lån;
    for (int i = 0; i < år; i++) {
        innbet[i] = (lån/år)+ (rente/100)*(remainderLoan);
        remainderLoan -= innbet[i];
        
    }
    return vector<double> (innbet);
}
First be careful when using non-ASCII characters in your source code, not all compilers will support those characters.

Next why are you trying to construct a "new" vector to return, why not just return innbet?

I have removed that now, having only
return innbet in line #10. However, when I run the code from main:

1
2
3
4
5
6
7
int main(){
cout << calculateSeries(5000, 10);
    
    
    
      return 0;
}

I get the following error message: Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream<char>') and 'Vector<double>')
There is no operator << for vector.
Ok, thanks. So how do I output my vector? Removing cout results in no output.
Loop through the vector and output each element.
Thanks, how would that look?
1
2
3
4
5
vector<double> vec = calculateSeries(5000, 10);
for (double val : vec)
{
	cout << val << endl;
}
Topic archived. No new replies allowed.