B
B
Bogdan Oleksandrovich2016-04-06 19:00:30
C++ / C#
Bogdan Oleksandrovich, 2016-04-06 19:00:30

How to remove an element from a class in C++?

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

  class tv
  {
  private:
    char mark[20];
    char model[20];
    char diagonal[20];
    char price[20];
  public:
    void input()
    {
      cout << "\nEnter of the mark: ";
      cin.get();
      cin.getline(mark, 20);
      cout << "Enter of the model: "; 
      cin.getline(model, 20);
      cout << "Enter of the diagonal: "; 
      cin.getline(diagonal, 20);
      cout << "Enter of the price: "; 
      cin.getline(price, 20);
    }
    void output()
    { 	
      cout << "Mark: "<< mark<< endl;
      cout <<"Model: " << model<< endl;
      cout  <<"Diagonal: " << diagonal << endl;
      cout <<"Price: " << price<< endl << endl;
    }
    void clearscr()
    {
      system ("cls");
    }
  };
int main(int argc, char** argv) 
{ 	
  int k, n, count;
  cout << "How many enter of the date?\n";
  cin >> n;
  tv *s = new tv[n];

  char m;
  do
  {
    cout << " 1 - Enter of the date\n" << " 2 - output\n "<< "3 - clear "<<"\n4 - delete"<< endl;
    cin >> k;
    
  switch (k)
  {
    case 1: for (int i = 0; i < n; ++i)
          s[i].input();
    break;
    case 2: for (int i = 0; i < n; ++i)
          s[i].output();
          break;
    case 3: s[0].clearscr();break;	
    case 4: 	cout << "\nEnter index of array which will be delete: " ;
        cin >> count;
      for (int i = count; i < n-1; ++i)
      {
        s[i]= s[i+1];
        
      }
    default: cout << "You have entered not the correct number" << endl;
  }
  cout << "Do you want go out?(y/n)"<< endl;
  cin >> m;
    }
  while(m == 'y');
  
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arseniy Efremov, 2016-04-06
@idbogdanbabitskiy

There is no "<<" operator overload for ostream that takes class tv as a parameter. Try defining a toString() method in the tv class that will display what you want on the screen.
In addition, in the tv class you have not overloaded the "=" operator, so the assignment is superficial. I don’t remember how it is with static arrays, but, most likely, they simply start referring to another memory area (that is, to strings in an adjacent element). Therefore, if you did not have elements older than count initialized with something, then everything will be filled with garbage.
Another point is that the array doesn't actually shrink, it's you moving the elements from right to left, leaving one unused at the end. Usually in such cases, memory is allocated for a new array of a smaller size, and then everything is transferred to it, for example.

B
Bogdan Olexandrovich, 2016-04-06
@idbogdanbabitskiy

In case 4 , I did, but when I display it, an error pops up. How to get it out? And did I do it right?2f2d1894f66a4997a39add5f50f7e0c4.JPG

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question