Why range error & how to fix it in this program?
Hi,
I can't figure out why it gives the warning "range error". The program doesn't run... Could you please give a hint for fixing it? Thanks a lot...
Code:
// Exercise 8.5
#include "std_lib_facilities.h"
/*
write a function to reverse the order of elements in a vector
for example
1 2 3 4 5 6
becomes
6 5 4 3 2 1
*/
void v_rev1(vector<double>&);
void print(vector<double>&);
int main()
try {
cout << "Enter some numbers: ";
vector<double> v;
double x;
while (cin >> x)
v.push_back(x);
cout << "In reversed sequence: " << '\n';
cout << "{";
v_rev1(v);
print(v);
cout << "}" << '\n';
}
catch (runtime_error e) {
cout << e.what();
}
void v_rev1(vector<double>& v1)
{
vector<double> v2;
for (unsigned int i = v1.size() - 1; i >= 0; --i) {
v2.push_back(v1[i]);
}
v1 = v2;
}
void print(vector<double>& v)
{
for (unsigned int j = 0; j < v.size(); ++j) {
cout << v[j];
if (j != v.size() - 1)
cout << ", ";
}
}