0

Alright, I know that may sound confusing - I'm an new one on the idea of programming. I have a CNC project that will take values from a text file, assign them, and transmit them via a serial connection to an Arduino which will receive and drive the motors, so on, so forth.

for( std::string line; getline( input, line ); ) 
{
int x, y;
input >> x >> y;
}

But, I want to be able to have the program process any length of a text file - any number of coordinates. In the interface I am designing a entry panel that would allow the user to specify the number of the commands. But, how do I introduce code that would take that number of commands and introduce that number of variables? I understand I could brute-force this by creating 1000 variables of each X, Y, Z, and other command types, and have up to 1000 possible line processing, but it would be much more efficient to have code that realizes this and adjusts for me.

Say, for example, I have that text entry box output a value designated NumberOfCommands. How would I tell the program to create a number of X-axis, Y-axis, and Z-axis (as well as other serial) commands where that number is equal to NumberOfCommands?

taocp
  • 23,276
  • 10
  • 49
  • 62
ecfedele
  • 306
  • 3
  • 15
  • 2
    You probably want to find a [decent beginner's book](http://stackoverflow.com/q/388242/179910) and read about `std::vector` about now. – Jerry Coffin May 24 '13 at 23:16

2 Answers2

3

You can use the std::vector class to store any number of elements.

So in your case something like this :

struct Coordinate {
    int x,y,z;
};

std::vector<Coordinate> coords;

for( std::string line; getline( input, line ); ) 
{
   Coordinate coord;
   input >> coord.x >> coord.y >> coord.z;
   coords.push_back(coord);
}

Or with emplace_back:

struct Coordinate {
    Coordinate(int x, int y, int z):x(x),y(y),z(z){ }
    int x,y,z;
};

std::vector<Coordinate> coords;
int x,y,z;    
for( std::string line; getline( input, line ); ) 
{
   input >> x >> y >> z;
   coords.emplace_back(x,y,z);
}

emplace_back doesn't make a copy like push_back does, it creates and puts the element in the vector.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • Now, in the form x(x),y(y),z(z); is the x/y/z notated in parenthesis the number? That is, could I notate in the code serial.write(x(4)) for the fourth set of coordinates? – ecfedele May 25 '13 at 07:29
  • The `x` inside the paranthesis is the parameter `x` passed to the constructor, which is the first parameter of `emplace_back`. The `x` outside the paranthesis is the member variable `x`. When you are sending it to `serial.write` you will do `serial.write(coords[0].x)` to send the 0th element's `x` member. – Barış Uşaklı May 25 '13 at 14:27
0

You could use arrays that are sized dynamically.

for example from here:

int *myArray;           //Declare pointer to type of array
myArray = new int[x];   //use 'new' to create array of size x
myArray[3] = 10;        //Use as normal (static) array
...
delete [] myArrray;     //remember to free memory when finished.

The question is where does x come from? You could assume 1000 and keep a count as you fill the array. Then if you get more info than that size you could resize the array.

Or you could start with a entity to start with like an STL vector<int>

syam
  • 14,701
  • 3
  • 41
  • 65
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 1
    This is C++ not C. Please don't advise using arrays, especially combined with naked pointers: this is the best way to screw things up. – syam May 24 '13 at 23:29