Trying to "print" out a two-dimentional for loop

What is wrong with my code? It doesn't output anything to the console.

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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int r = 4;
const int c = 4;
void showStars (string [r][c]);
int main() {

    string stars[r][c];
    
    showStars(stars);
    return 0;
}
void showStars (string stars [r][c])
{
    stars[r][c] = {"*"}; // works when I change r and c to 3.
    
    for (int i = 0; i < r; i++){
        
        for (int j = 0; j < c; j++)
            cout << stars[i][j] << "\t"; //works when I change i and j to 3; idk why.
    }
    cout << endl;
}
Last edited on
closed account (SECMoG1T)
Initialize your array at line ten with what you want '*' , remove line seventeen . That's all you need
The reason you get nothing in print is because in the declaration on line 10, your array is default intialized to empty strings "" , if you print that you get nothing . Solution to this is to intialize your array to * in the range r, c .
Last edited on
thanks for the reply andy

when I try your solution all I get is one star instead of 4/row and 4/column.

should look like :
* * * *
* * * *
* * * *
* * * *

any clue as to whats up?
Last edited on
closed account (SECMoG1T)
Line ten tyr this

1
2
3
4
5
6
////example
 string array [4][4]={ {"*","*","*","*"},
                       {"*","*","*","*"},
                       {"*","*","*","*"},
                       {"*","*","*","*"}
                      }
Last edited on
fixed it using this:
1
2
3
4
5
6
for (int i = 0; i < r; i++){
        cout << endl;
        for (int j = 0; j < c; j++){
            stars[i][j] = {" * "};
            cout << stars[i][j];
        }
Topic archived. No new replies allowed.