Print a Square with function using nested loop

The question is this:
a = square, b = Z, c = X
You need to input a or b or c and the size of the shape.
It is required to use function with nested loops

Imma wondering whats the problem with my first part of code
It print nothing... pleaseeee helppp

#include <iostream>
using namespace std;

void print(int a){
int size;
cin >> size;
for (int r=1; r<=size; r++){
for (int c=1; c<=size; c++){
if (r==1 || c==1 || r==size || c==size)
cout << "*";
else
cout << " ";
}
}
cout << endl;
}
int main (){
int input, size;
cin >> input;
cin >> size;
if (input == 'a')
print(size);




return 0;
}
on your code, 'input' variable is a int.

if you insert 'a' is char then you have the problem.

on function print you declare another int size..
is this what you really wanted to do? otherwise that command line is useless since you are passing 'size' through the function parameter
1
2
3
4
5
6
7
8
9
10
int main (){
	char input ;  // change from int to char
	int size ;
	cin >> input;
	cin >> size;
	if (input == 'a')
		print(size);

	return 0;
}



a
6
7
********     **     **     **     **     ********
Program ended with exit code: 0
Last edited on
@ar2007
Thank you so much for ur reply
I get the part changing int to char, i know i declare int size twice but if i didnt in the function part,
It didnt read the size... what can i do?

The programme requires
E.g. you need to input e.g. "a" (the square) and 4 (the size)
Then output
****
* *
* *
****
Last edited on
you define:

1
2
3
4
void function print( int a )
{
      // code
}


the parameter 'a', is the copy of 'size' when you call the 'print (size)' function inside the 'main' function..
then a == size. ( then the declaration 'int size' on the void function 'print' is useless ).

replace 'size' with 'a', on 'print' function.

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
#include <iostream>
using namespace std;

void print( int a )
{
	for ( int r = 1; r <= a; r++ )
	{
		for ( int c = 1; c <= a; c++)
		{
			if ( r == 1 || c == 1 || r == a || c == a )
				cout << "* ";
			else
				cout << "  ";
		}
		cout << endl;
	}
}


int main (){
	char input ;
	int size ;

	cin >> input; // don't understand this but is your code
	cin >> size;

	if ( input == 'a' )
		print(size);

	return 0;
}



a
6
* * * * * * 
*         * 
*         * 
*         * 
*         * 
* * * * * * 


reading your problem, I think you have misinterpreted the role of square ...
I think you should have inserted int a = 49, then int size = sqrt (49).
therefore 'a' is not the name of the square, but the value of the surface of the square, from which to derive the value of the side ...........
in this case your code becomes:


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

#include <iostream>
#include <math.h> // sqrt() function

using namespace std;

void print( int a )
{
	for ( int r = 1; r <= a; r++ )
	{
		for ( int c = 1; c <= a; c++)
		{
			if ( r == 1 || c == 1 || r == a || c == a )
				cout << "* ";
			else
				cout << "  ";
		}
		cout << endl;
	}
}


int main ()
{
	int square = 0 ;
	int size = 0 ;

	cin >> square ;

	size = sqrt( square ) ;

	print( size ) ;
	
	return 0;
}



49
* * * * * * * 
*           * 
*           * 
*           * 
*           * 
*           * 
* * * * * * * 
Last edited on
@ar2007
Yesssss!!! You explained it very wellllll !!! I get ittt!!
Thank you so so so muchhhhhh
Topic archived. No new replies allowed.