For my simple robot I want to store direction-and-distance in an array:
//Pseudo-code:
int move[dir][dist]; // dir and dist will both be integers
but I can't figure how to set it up.
Whatever I have tried, I get this error message from IDE:
Simple_robot:31: error: declaration of 'move' as multidimensional array must have bounds for all dimensions except the first
It would help if you posted the offending code, although I suspect perhaps that you are trying to define a 2D array with variables declaring the size of the dimensions, which the compiler does not like. This does not compile:
What I hope to do is read a direction from a keypad, and a distance from a (second) keypad, and store them as a 'move'. Repeat for the next move, etc. Then 'playback' the series of moves.
At first I tried:
int move[][] = {0,1,2,3},{999,999,999,999};
representing the known directions 0,1,2 or 3, and some arbitrary numbers in the 2nd dimension.
Error message, as in my first post,
Simple_robot:31: error: declaration of 'move' as multidimensional array must have bounds for all dimensions except the first
In your code, what do 'a' and 'b' represent? I don't really want constants, do I?
And to make it worse, I've just realised I shall need 3-dimensions as in move[1][dir][dist] etc.
Whoops! I shouted too soon. Whilst I can now create the array-object, I'm stuck with entering data to it.
// array [moveNr][dir][distance]
int move[][1][1] = {{},{0},{999}};
And now I want to set the 'dist' into the last part of array-item number 'moveNr'
void how_far()
{
if(readNumPadNow ==1)
{
num_key_in = read_numberPad(x);
do
{
x ++; // x==0 is already taken by the LCD keypad
num_key_in = read_numberPad(x); // read the numeric pad
if(x>3)
{x=0;}
} while( num_key_in <=180 );
readNumPadNow =0;
dist = calculate_Number(x, num_key_in);
move[moveNr][][2] = dist; // Which will not compile
x =0;
}
}
The error I get is this
Simple_robot.cpp: In function 'void how_far()':
Simple_robot:87: error: incompatible types in assignment of 'int' to 'int [1]'
Then somewhere else I want to set the 'middle' part of the same item to 'dir' which can be 0 or 1 or 2 or 3 (as N, E, S, W)
Geoff
You can't set an entire row/column/plane of an array except at initialization time. After initialization is complete, you can only assign values one element at a time, unless you really know what you are doing (and the array is organized correctly for your purposes).
Hi Paul,
I think that is exactly what I want to do ...
// initially, move[] is empty as in
move[0][0]0;
move[1][0][0];
.
.
move[whatever][0][0];
// later to be filled, one dollop at a time
so move[0].. becomes move[0][not-yet-set][42];
// later, when I have ascertained it from the keypads, it becomes move[0][3][42];
I'm attempting to make a robot, which young, non-geek children can 'program' to navigate wherever. Each move will be entered e.g. Forward 42; Left 90; Forward 8 ... and so on. The direction part will come from the keypad on the DFRobot LCD-keypad Shield, and the distance part from my own, home-made, numeric pad.
If all the moves are 'stored' in an array, they can be 'played-back' by stepping through it:-
e.g.
moveNr, dir, dist
First Move : Right, 42 (stored as 0 2 42)
Second move: Fwd, 19 (stored as 1 0 19)
(Much) later, I hope to code so that the child/user can Edit their moves, i.e. Move#13 should have been Left, Distance is OK.
or a vector or one-dimensional if you use an array of "struct"s instead of the second dimension.
whatever it meant, I think I've got my head around it at last:
at appropriate places I 'learn' what values are wanted:-
tempDir = 0; // (or 1, or 2, or 3)
tempDist = 42;
....
then, when user is happy, they press 'select' (or maybe I'll call it 'OK'), which triggers
Moves[moveNr][0] = tempDir;
Moves[moveNr][1] = tempDist;
moveNr ++;
Now I can move on to the next task in this project.