Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ dynamic array of structures

Tags:

c++

Could you help me organize a dynamic array of points?

I have coped with a dynamic array of integers. But I don't know how to organize it with structures.

Here is my code so far...

#include "stdafx.h"
#include <cstdlib>;
#include <iostream>;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  int i = 0; // Array index.
  struct Point
  {
    int x;
    int y;
  };
  size_t n = sizeof(int);
  int * points = static_cast<int*>(malloc(n));
  char command;
  do
  {
    cout << "n - Create new point." << endl;
    cout << "q - Quit." << endl;
    cout << "Input a new command: ";
    cin >> command;
    if (command == 'n')
    {
      points[i] = 1;
      i++;
      /* points[i] = new Point();
         points[i].x = 1;
         points[i].y = 1; */
      // cout<<"("<<point1.x<<","<<point1.y<<")";               
    }               
    else if (command == 'q')
    {       
      for (int j = 0; j < i; j++)
        cout << points[j] <<endl;
      system("pause");
      return 0;
    }
    else
    {
      cout << "Please, enter a correct command." << endl << endl << endl;               
    }
  } while (true);
  system("pause");  
  return 0;
}
like image 568
Kifsif Avatar asked Jul 18 '26 10:07

Kifsif


1 Answers

look into vectors

#include <vector>

http://www.mochima.com/tutorials/vectors.html

like image 72
iedoc Avatar answered Jul 21 '26 02:07

iedoc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!