Printing a Square

Hey guys
im trying to make a program that recieves a number and makes a square using X's in that size.

Cant get it right....any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

using namespace std;

int main()
{
	char space(' ');
	int a,b;
	b = 0;
	cin >> a;
	for (int i = 0; i < a; i++)
		cout << "x" << " ";
	
	for (int h = 0; h < a; h++)
	{
		cout << endl << "x";
		for (int g = 0; g < a; g++)
			cout << space << "x";
	}
	for (int j = 0; j < a; j++)
		cout << "x" << "  ";
	return 0;

}
Hello ofiryak1994,

You have one to many for loops. The loop at line 20 and 21 is not needed. And the lines 11 - 19 need to b nested for loops. The for loop at lines 17 and 18 are not needed either.

You define an int 'b', but never use it and you should initialize variable 'a' int a{ 0 }; or whichever way you like. It is just a good practice.

In line 16 remove the endl and put a space after the 'x' which makes line 7 unneeded.

Hope that helps,

Andy
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()
{
	char s = ' ';
	char o = '*';
	int size;

	cout << "Please Enter Size: "; 
	cin >> size; 
	cout << endl << endl; 

	for (int i = 0; i < size; i++)
	{
		cout << o;
		for (int j = 0; j < size; j++)
		{
			
			if (j == size-1 || i == 0 || i == size-1 )
				cout << o;
			else
				cout << s;
		}
		cout << endl; 
	}

}
Topic archived. No new replies allowed.