You are using a version without Ads of this website. Please, consider donating:

### input to Multidimensional array help :s

let's say i create a 2D array

float num[3][3];

and i ask the user to input two numbers (the row and then the collum)

cout << "Please enter the row to enter a number to" << endl;
cin >> row;
cout << "Please enter the collum to enter a number to" << endl;
cin >> collum;

the user could then enter (E.G. 2 & 3) so row = 2 and collum = 3
how would i take these numbers and make it so that in the selected cell of the array the it automatically enters a "X"? with the 2 & 3 the number would be at the bottom of teh center collum.
Last edited on
if you want to enter "X" you can't use a float array.
just use std::string

the element is used like this
`num[row][column];`

keep in mind: a 3x3 array goes from [0][0] to [2][2]
Arrays are numbered from 0 to n-1, in your case 3 would be out of bounds. But let's say the user has entered row=1 and column=2 (which would translate to row 2, column 3).

To insert data there you use the [] operator as such: num[row][column] = //...user data;
@GothicFighter: There is not really a out of bounds with arrays. for example:
 ``12345678910111213`` ``````int main() { int A[1]; A[0] = 0; A[1] = 1; A[2] = 2; A[3] = 3; int a = A[3]; int b = A[2]; int c = A[1]; }``````

 ```a=3 b=2 c=1```

This works fine, you can try it ;)
ok thanks for your help :D
@Darkmaster
the code works fine but it saves the values to the locations just after A and overrides whatever is in there and is of course not safe, it can delete important values from the memory.
You can just do like this
 ``12345678`` ``````float num[3][3]; int row , collum; // Spelling of column is incorrect cout << "Please enter the row to enter a number to" << endl; cin >> row; cout << "Please enter the collum to enter a number to" << endl; cin >> collum; cout<<"Now enter the number you want to put in the cell"<> num[row-1][collum-1];``````
Darkmaster wrote:
This works fine, you can try it ;)
 ``12345678910111213`` ``````int main() { int A[1]; A[0] = 0; A[1] = 1; A[2] = 2; A[3] = 3; int a = A[3]; int b = A[2]; int c = A[1]; }``````

On compilation:
 `warning C4789: buffer 'A' of size 4 bytes will be overrun; 4 bytes will be written starting at offset 12 `

On running:
 `Run-Time Check Failure #2 - Stack around the variable 'A' was corrupted.`

If by "works fine" you mean "evokes undefined behavior and may cause a segmentation fault," then we're in agreement.
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: