I need to figure out how to get a string of user input into a 2d array. The array is to be a square (equal number of rows and cols). The user is to input the size of the array. I know about using nested loops to get input into a 2D array.
Here's the catch though - The user is to input each row as a string of numbers with spaces, like so (if the user enters 5 for the size of the array):
1 2 3 4 5
(this would be the input for one row. this would be repeated for 5 rows)
Some how I have to break up that string and put the numbers into the proper row of the array.
Every example and tutorial I can find on putting user input into a 2D array has the user inputing one element at a time (and hitting return after each input). I looked at the reference above. But, no matter what I try I can't figure out how to accept a string of integers with spaces in between. Here's some code I wrote.
That does read the values properly. The problem is in the output that seems misplaced.
Add cout<<"\nGot ("<<i<<','<<j<<") "<<square[i][j]<<endl; after line 28 and you will get:
Enter row size: 2
Enter Data in square
Enter row [0]: 1 2 3 4
Got (0,0) 1
Enter row [0]:
Got (0,1) 2
Enter row [1]:
Got (1,0) 3
Enter row [1]:
Got (1,1) 4
You have a more serious error on line 14: size of static array must be known during compilation. The size of square is undefined.