2d array how to split string into characters

how to split strings in 2d array and also accept white spaces. for example if i typed in input: the <- it will split it into |t| |h| |e| and will be placed in different index.

the output should be like this.

1
2
3
4
  0  1  2  3  4  5  6  7  8  9
0 1  2  3  4  5  a  b  c  d  e
1 f  g  h  i  j  k  l  m  n  s
2 t  h  e


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include<iostream>

std::string myarray[3][10] = {{"1","2","3","4","5","a","b","c","d","e"},
						{"f","g","h","i","j","k","l","m","n","s",}, 
						{" "," "," "," "," "," "," "," "," "," ",}};
							
void displaygrid();

using namespace std;

int main (){

	
	int size=0

	string add; //
	
	    displaygrid();
	    cout<<"==========================================" <<endl;
	    cout<<"insert element: ";
	    cin>>add;
	    myarray[2][size] = add;
	    cout<<"Insert Successful!" <<endl;
	    size=size+1;
		displaygrid();
	
		
}

void displaygrid(){

	cout<<endl;
	for(int z = 0; z<10; z++){
		cout<<"  "<<z;
	}
	cout<<endl;
	
	for(int x = 0; x<3; x++){
	cout<<x;
		for(int y = 0; y<10; y++){
			cout<<" " <<myarray[x][y] <<" ";
		}
	cout<<endl;
	}
}


is it possible? thanks in advance
Last edited on
cin>> noskipws >> add;

You are doing some very dubious things with this line, though.
myarray[0][size] = add;
Why don't you just use a 1-d array?

Also, the title of your thread ... doesn't live up to expectations.
Last edited on
thanks for reply. actually, i could just put myarray[2][0] and i need to use 2d array for my text editor and the reason than i use that code is that when i "erase" some values in my array, i just want the position of add in the right place where the last value is. i know its a poorly attempted logic, sorry about that.
Last edited on
I agree with lastchance. A 1D array is much eaiser. If you see a string as an array of chars you basically can treat it like a 2D array. Also would be a good idea to change the title of the thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

std::string myarray[] = 
{ 
  "12345abcde",
  "fghijklmns",
  "          "
};

void displaygrid();

using namespace std;

int main() {


  int size = 0;

    char add; //

  displaygrid();
  cout << "==========================================" << endl;
  cout << "insert element: ";
  cin >> add;
  myarray[2][size] = add;
  cout << "Insert Successful!" << endl;
  size = size + 1;
  displaygrid();


}

void displaygrid() 
{
  cout << endl;
  for (int z = 0; z<10; z++) 
  {
    cout << "  " << z;
  }
  cout << endl;

  for (int x = 0; x<3; x++) 
  {
    cout << x;
    for (int y = 0; y<10; y++) {
      cout << " " << myarray[x][y] << " ";
    }
    cout << endl;
  }
}


Output:


  0  1  2  3  4  5  6  7  8  9
0 1  2  3  4  5  a  b  c  d  e
1 f  g  h  i  j  k  l  m  n  s
2
==========================================
insert element: @
Insert Successful!

  0  1  2  3  4  5  6  7  8  9
0 1  2  3  4  5  a  b  c  d  e
1 f  g  h  i  j  k  l  m  n  s
2 @
Topic archived. No new replies allowed.