How do I convert a row in a 2D array to a string

Hi, I've written a code which let's the user input the dimensions of a grid which they will then put in numbers in the grid (0,1 and 2). 0 represents and empty space, 1 an unlighted bomb and 2 a lighted bomb.

The final output would be what would happen to the grid when the lighted bombs (2) explodes (like an unlighted bomb on top of 2 would fall and replace the position of 2). I'm assuming the input would be correct and that no one would violate the rules.

I've made the code and it works, but I want the values printed out to be in a string for each row instead of an int for each grid.

for example a row would be 1 0 0 which I would print out number by number through the 'for' loop, but how do I convert that 3 values into 1 string?

I hope I'm making myself clear. Really new at programming in general so not clear on what info is needed, don't hesitate to ask.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
using namespace std;
int main(){
    int sizes[2];
    string values;
    cout<<"Insert two numbers separated by spaces here. Do not violate this rule."<<endl;
    for (int i=0; i<2; i++){
        cin>>sizes[i];
    }
    int *x, *y;
    x=&sizes[0];
    y=&sizes[1];
    int grid[*x][*y];
    cout<<"Grid is "<<*y<<" number of rows and "<<*x<<" number of columns."<<endl;
    for (int i=0; i<*y; i++){
    cout<<"Type in "<<*x<<" numbers. Separated by spaces. use only 0, 1 or 2"<<'\n'<<endl;
            for (int j=0; j<*x; j++){
    cin>>grid[i][j];
    cout<<"Your "<<*x<<" numbers are: "<<grid[i][j]<<'\n'<<endl;
       }
    }
    cout<<"Your grid number is:"<<'\n'<<endl;
    for (int i=0; i<*y; i++){
        for (int j=0; j<*x; j++){
            cout<<grid[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<"Now, assuming all those numbers in the grid are empty spaces and bombs."<<'\n'
    <<"0 is an empty space, 1 is an unlighted bomb and 2 is a lighted bomb,"<<'\n'
    <<"How would the grid look like after all the bombs have 'Kaboom'-ed?"<<'\n'<<endl;

    for (int i=0; i<*y; i++){
        for (int j=0; j<*x; j++){
            if (grid[i][j]!=1){
                grid[i][j]=0;}
        }
    }


    for (int i=0; i<*x; i++){
    int up = 0, down = *y-1;
    while (up < down)
    {

        while (grid[up][i] == 0 && up < down)
            up++;


        while (grid[down][i] == 1 && up < down)
            down--;

        if (up < down)
        {
            grid[up][i] = 0;
            grid[down][i] = 1;
            up++;
            down--;
        }
    }
    }

    cout<<"Your new grid number is:"<<'\n'<<endl;
    for (int i=0; i<*y; i++){
        for (int j=0; j<*x; j++){
            cout<<grid[i][j]<<" "; //this is what I want to change
        }
        cout<<endl;
    }
}



been looking around, I've tried using ostream or stringstream but maybe due to my inexperience, I couldn't convert each row into a string. Another thing is that the dimension is determined only on input so I can't use a fixed number of strings. Hope it's doable.

Also, if there are changes that can be made to improve the overall code, suggestions would be good.

Thanks a lot!
Last edited on
Are you just wanting to get rid of spaces between the numbers? Or is your end goal to have the entire board in a single string? std::stringstream is certainly the way to go for converting numbers to std::string
Last edited on
Are you just wanting to get rid of spaces between the numbers? Or is your end goal to have the entire board in a single string? std::stringstream is certainly the way to go for converting numbers to std::string


Thanks for replying, and sorry for the late reply.

Actually, I want the spaces in between the numbers. My end goal is to have each row of the grid to be one string.

eg. a 3x3 grid, I would need 3 strings, each representing one row of the grid, with spaces in between the numbers to separate them and for easy viewing of the grid.

But I'm not sure how to convert a 2D array into an 'n' number of strings (Determined by the number of rows), each string representing a row of the grid.

Well for starters we need a container that can hold n strings:
std::vector<std::string> strs;

Then using std::stringstream for number conversion is what I usually do.
1
2
3
4
5
6
7
8
for(int i=0; i<height; i++) {
   std::stringstream ss;
   ss << board[i][0];
   for(int j=1; j<width; j++) {
      ss << ' ' << board[i][j];
   }
   strs.push_back(ss.str());
}


Looping over strs can be done like this:
1
2
3
for(size_t i=0; i<strs.size(); i++) {
//do something with strs[i]
}

Or if you prefer the newer C++11:
1
2
3
for(auto& str : strs) { //the & allows modifying the current spot in the array
//do something with str
}


Almost forgot to remind you:
1
2
#include <vector>
#include <sstream> 
Last edited on
Whoa, that did the trick, thanks a lot for the help!
Topic archived. No new replies allowed.