title: C ++ - Problem saving values - values change after saving
body: <p>I've been trying for a few days to solve a memory problem.</p>
  
  <p>I have a program where the user enters a vector and its name.</p>
  
  <p>The program stores everything in the vector class.</p>
  
  <p>The problem is that when I print the vector, the values change to other values. </p>
  
  <p>I mean an array whose name is "<em>array</em>" in <em>Vector::Vector</em></p>
  
  <p>Does anyone know where the problem might be?</p>
  
  <p><strong>Main.cpp:</strong></p>
  
  <pre><code># include "Midgam.h"
  
  int main(int argc, char **argv) {
      Midgam m(3,20),m1();
      m.Start();
      m.~Midgam();
      return 0;
  }
  </code></pre>
  
  <p><strong>Midgam.cpp:</strong></p>
  
  <pre><code>#include "Midgam.h"
  
  Midgam::Midgam(int num_of_boxes, int num_of_parties):boxNum(num_of_boxes),maxParties(num_of_parties) {
      iterator = 0;
      midgam = new Vector[maxParties];
  }
  
  Midgam::~Midgam() {
      if(midgam)
          for(int i=0; i &lt; iterator; i++)
              midgam[i].~Vector();
      delete [] midgam;
  }
  
  void Midgam::Start(){
      int choice;
      while (choice != 6){
          cout &lt;&lt; "Please enter your choice" &lt;&lt; endl;
          cin &gt;&gt; choice;
          switch (choice) {
          case 1:
              AddParty();
              break;
          case 2:
              cout &lt;&lt; "case 2" &lt;&lt; endl;
              break;
          case 3:
              cout &lt;&lt; "case 3" &lt;&lt; endl;
              break;
          case 4:
              cout &lt;&lt; "case 4" &lt;&lt; endl;
              break;
          case 5:
              cout &lt;&lt; "case 5" &lt;&lt; endl;
              break;
          case 6:
              break;
          default :
              cerr &lt;&lt; "ERROR: invalid command; type 6 for exit" &lt;&lt; endl;
          }
      }
      cout &lt;&lt; "END OF PROGRAM" &lt;&lt; endl;
  }
  
  void Midgam::AddParty(){
      string name;
      cout &lt;&lt; "Print All:" &lt;&lt; endl;
      PrintAll();
      cout &lt;&lt; "Insert name:" &lt;&lt; endl;
      cin &gt;&gt; name;
      if (name.size() &gt; 12){
          cerr &lt;&lt; "Party name is too long, please don't pass the 12 characters!!!" &lt;&lt; endl;
          return;
      }
      cout &lt;&lt; "Insert values:" &lt;&lt; endl;
      Vector v(name,boxNum);
      if (!v.getBool()){
          cerr &lt;&lt; "ERROR: invalid input" &lt;&lt; endl;
          return;
      }
      int exist = FindPartyByName(name);
      cout &lt;&lt; "exist: " &lt;&lt; exist &lt;&lt; endl;
      if(exist != -1){
          cout &lt;&lt; "EXIST" &lt;&lt; endl;
          midgam[exist]=v;
          return;
      }
      cout &lt;&lt; "iterator: " &lt;&lt; iterator &lt;&lt;endl;
      if(iterator &gt;= maxParties){
          cerr &lt;&lt; "ERROR: no more new parties" &lt;&lt; endl;
          return;
      }
      cout &lt;&lt; "iteraator: " &lt;&lt; iterator &lt;&lt; endl;
      midgam[iterator]=v;
      iterator++;
  }
  
  int Midgam::FindPartyByName(string party){
      for (int i=0; i &lt; iterator; i++){
          if (party.compare(midgam[i].getName()) == 0){
              return i;
          }
      }
      return -1;
  }
  
  void Midgam::PrintAll(){
      for(int i=0; i &lt; iterator; i++){
          cout &lt;&lt; midgam[i].getName() &lt;&lt; endl;
          midgam[i].PrintArray();
      }
  }
  </code></pre>
  
  <p><strong>Midgam.h:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "Vector.h"
  
  using namespace std;
  
  #ifndef MIDGAM_H_
  #define MIDGAM_H_
  
  class Midgam {
  private:
      int boxNum;
      int maxParties;
      int iterator;
      Vector *midgam;
  public:
      Midgam(int num_of_boxes, int num_of_parties);
      virtual ~Midgam();
      void Start();
      void AddParty();
      int FindPartyByName(string party);
      void PrintAll();
  };
  
  #endif /* MIDGAM_H_ */
  </code></pre>
  
  <p><strong>Vector.cpp:</strong></p>
  
  <pre><code>#include "Vector.h"
  
  Vector::Vector(string name,int size):name(name),size(size){
      array = new unsigned int[size];
      string results;
      cin.ignore();
      getline(cin,results);
      Bool = StringToArray(results);
  }
  
  Vector::Vector(const Vector &amp;v2){
      name=v2.name;
      size=v2.size;
      Bool=v2.Bool;
      array = new unsigned int[size];
      for(int i=0; i &lt; size; i++)
          array[i]=v2.array[i];
  }
  
  Vector::Vector(){
      name="";
      size=0;
      array=NULL;
      Bool=true;
  }
  
  Vector::~Vector() {
      if(array)
          delete [] array;
  }
  
  bool Vector::StringToArray(string str){
      stringstream ss(str);
      for(int i=0; i &lt; size ; i++){
          if(ss.eof())
              return false;
          ss &gt;&gt; array[i];
          cout &lt;&lt; array[i];
      }
      if(!ss.eof())
          return false;
      return true;
  }
  
  bool Vector::getBool(){
      return Bool;
  }
  
  string Vector::getName(){
      return name;
  }
  
  void Vector::PrintArray(){
      for(int i=0; i &lt; size; i++)
          cout &lt;&lt; array[i] &lt;&lt; " ";
      cout &lt;&lt; endl;
  }
  </code></pre>
  
  <p><strong>Vector.h:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  #ifndef VECTOR_H_
  #define VECTOR_H_
  
  class Vector {
  private:
      string name;
      int size;
      unsigned int *array;
      bool Bool;
  public:
      Vector(string name,int size);
      Vector();
      Vector(const Vector &amp;v2);
      virtual ~Vector();
      bool StringToArray(string str);
      bool getBool();
      string getName();
      void PrintArray();
  };
  
  #endif /* VECTOR_H_ */
  </code></pre>