Why can't I make this 6x6 square?

Below is the code. I can't seem to get that column of "F"s on column 4 to column 6 to make it into a 6x6 square..

Any help is appreciated.
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>

using namespace std;
int main(int argc, char *argv[]) {
	
	string check[6][6];
	
	for (int k = 0; k <= 5; k++){
		check[0][k] = "F";
		check[5][k] = "F";
	}
	for (int k = 0; k < 4; k++){
		check[k+1][0] = "F";
		check[k+1][5] = "F";
	}
	
	for (int i = 0; i < 6; i++){
		for (int j = 0; j < 6; j++){
			cout << check[i][j] << " ";
		}
		
		cout << endl;
	}
}
It's a visualization issue. The cells that you didn't set anything are empty "" they do not contain a blank " "
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
#include <iostream>

using namespace std;
int main(int argc, char *argv[]) {

	string check[6][6];

    for (int i = 0; i < 6; i++){
		for (int j = 0; j < 6; j++){
			check[i][j] = " ";
		}
	}

	for (int k = 0; k <= 5; k++){
		check[0][k] = "F";
		check[5][k] = "F";
	}
	for (int k = 0; k < 4; k++){
		check[k+1][0] = "F";
		check[k+1][5] = "F";
	}

	for (int i = 0; i < 6; i++){
		for (int j = 0; j < 6; j++){
			cout << check[i][j];
		}

		cout << endl;
	}
}

You can also do this to simplify and make your code faster. Too much for loops can make your code slower. BTW static_cast<void>() is a casting used in c++. I used this to prevent the compiler from detecting unused variable argc, and argv.

You can use this code to make a square by just modifying the row and/or the column variable.

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>

int main(int argc, char *argv[])
{
    static_cast<void>(argc);
    static_cast<void>(argv);

    int row = 6;
    int column = 6;

    std::string check[row][column];
    std::string value;

    for(int i = 0; i < row; ++i)
    {
        for(int j = 0; j < column; ++j)
        {
            value = ((i == 0 || j == 0 || i == (row - 1) || j == (column - 1)) ? "F" : " ");
            check[i][j] = value;
            std::cout << check[i][j] << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}
Last edited on
Or you can use Square as a class.

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
#include <iostream>

class Square
{
public:
    Square(int side) : side_(side), line_("F"){}
    void setSide(int side){ side_ = side; }
    void setLine(std::string line){ line_ = line; }
    int getSide(){ return side_; }
    std::string getLine(){ return line_; }
    void drawSquare()
    {
        std::string square[side_][side_];
        for(int i = 0; i < side_; ++i)
        {
            for(int j = 0; j < side_; ++j)
            {
                square[i][j] = (i == 0 || j == 0 || i == (side_ - 1) || j == (side_ - 1)) ? line_ : " ";
                std::cout << square[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
private:
    int side_;
    std::string line_;
};

int main(int argc, char *argv[])
{
    static_cast<void>(argc);
    static_cast<void>(argv);

    Square mySquare(6);
    mySquare.setLine("X");
    mySquare.drawSquare();

    return 0;
}


Topic archived. No new replies allowed.