-1

I'm probably not using cin correctly here, but can someone tell me why this small program will not run and produce any output?

#include <iostream>
using namespace std;

class Vector {
public:
   Vector(int s) :elem{new double[s]}, sz{s} {}
  ~Vector() { delete[] elem; } //added this
   double& operator[](int i) { return elem[i]; }
   int size() { return sz; }
private:
   double *elem;
   int sz;
};

double read_and_sum(int s) {
   Vector v(s);
   for (int i=0; i != v.size(); ++i)
      //cin>>v[i];
      v[i]=s;
      cout<<"s = "<<s<<"\n";

   double sum = 0;
      for (int i=0; i != v.size(); ++i)
         sum+=v[i];
   return sum;
}

int main()
{
   int i = 0;
   cout<<"Please enter an integer: ";
   cin >> i;
   cout<<"Sum = "<<read_and_sum(i)<<"\n";
}
Andrew Hummel
  • 400
  • 2
  • 5
  • 22

1 Answers1

0

The program works perfectly fine. I personally think you are entering the wrong numbers. If you actucally wrote the code, you would know how it works but you say that you're new but I don't think someone that cant understand how someone cant read the code correctly be able to do vectors.

S A
  • 827
  • 10
  • 23