Fill box issue

Hello, im having trouble with the function calls of my program. i need the program to output a box of a given size of asterisks and have the interior of that box be filled with a character the user chooses
like: ****
*OO*
**** for instance would be a 4x3 square with o's but im having trouble with the function calls

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



void cinInRange (int low,int high);
void displayBox (int numRows, int numCols, char fillchar);
int main ()
{

	cinInRange(3,20);
	cinInRange(3,60);
	displayBox(//this is what im confused on)
	


}

void cinInRange (int low, int high)
{
	int input;
	
		cout << "Enter a number between " << low<< " and " << high << ": ";
		cin >> input;

	while (input<low || input > high)
	{
		cout << "Invalid entry." << "Enter a number between " << low << " and " << high << ": ";
		cin >> input;
	}
	
	return ;
}

void displayBox (int numRows, int numCols, char fillchar)
{
	cout << "Enter a character for the interior of the box: ";
	cin >> fillchar;
	numRows=numCols;
	for (int i=0; i <numRows ; i++)
	cout << '*';
	for (int j=0; j<numCols-2*fillchar; j++)
	{	
		cout << '*'<< endl;	
		cout << fillchar;
		
	}
	for (int i=0; i < numRows; i++)
		cout << '*';
	cout <<endl;
	return;

}

	
Last edited on
@paper32

Only had a few problems with it. Main one was numRows=numCols Kinda messed up a lot of things. Here is the program with the corrections and explanations of the workings, to help you better understand the logic.
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
// Filled Box.cpp : main project file.

#include <iostream>
#include <string>

using namespace std;

int cinInRange (int low,int high, string box); // Added string variable to let user know what the value to 
                                               // input is for
void displayBox (int numRows, int numCols);
int main ()
{
int rows,col;
	rows = cinInRange(3,20, "rows"); // rows takes value from variable 'input' in cinInRange 
	col = cinInRange(3,60, "columns"); // col takes value from variable 'input' in cinInRange
	displayBox(rows, col);
}

int cinInRange (int low, int high, string box)
{
	int input = 0;
	do  // Do/While loop to check for input boundaries
	{
		cout << "Enter a number between " << low << " and " << high << " for the " << box << " : ";
		cin >> input;
		if (input<low || input > high)
			cout << "Invalid entry. Enter a number between " << low << " and " << high << " only." << endl;
	}while (input<low || input > high);
	
	return input; // Sends inputted value back to calling section 
}

void displayBox (int numRows, int numCols)
{
	char fillchar;
	int i,j,k;
	cout << "Enter a character for the interior of the box: ";
	cin >> fillchar;
	// numRows=numCols;  Not useful - REMOVE
	for (i=0;i<numCols;i++)
		cout << '*'; // couts top of box
	cout << endl; // Starts center of box on a new line
	for (j=0; j<numRows-2;j++) // Subtract 2, 1 for top & 1 for bottom
	{	
		cout << '*';// couts left side
		for(k=0;k<numCols-2;k++)// Subtract 2, 1 for each side 
			cout << fillchar;
		cout << "*" << endl; // couts right side
	}
	for (int i=0; i<numCols; i++)
		cout << '*'; // couts bottom of box
	cout <<endl << endl; //  
	return;

}
Last edited on
Thanks man!, however, the function call for displaybox still needs three parameters, and its only being handed back rows,col but not the fill char. i need the void displaxBox (int numrows, int numcols, char fillchar) to stay within those parens. like i cant have
1
2
3
void displayBox (int numRows, int numCols)
{
	char fillchar;


i need void displayBox (intnumRows, int numCols, char fillchar)
how would i fit the char into int main?
int main()
{
int numRows, numCols;
char fillChar;

numRows = cinInRange ( 3, 20 );
numCols = cinInRange ( 3, 60 );

displayBox ( numRows, numCols, fillChar );

return 0;
}
@paper32

You need to create another function to get the char variable. Like so..
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
// Filled Box.cpp : main project file.

#include <iostream>
#include <string>

using namespace std;

int cinInRange (int low,int high, string box); // Added string variable to let user know 
// what the value to input, is for.
char GetFill();
void displayBox (int numRows, int numCols, char fillchar);

int main ()
{
	int rows,col;
	char fill;
	rows = cinInRange(3,20, "rows"); 
	col = cinInRange(3,60, "columns");
	fill = GetFill(); // Ask for displayBox fill
	displayBox(rows, col, fill); // Sends all 3 variables needed to create Box
}

int cinInRange (int low, int high, string box)
{
	int input = 0;
	do  // Do/While loop to check for input boundaries
	{
		cout << "Enter a number for the " << box << " between " << low << " and " << high << " : ";
		cin >> input;
		if (input<low || input > high)
			cout << "Invalid entry. Enter a number between " << low << " and " << high << " only." << endl;
	}while (input<low || input > high);

	return input;
}

char GetFill()
{
	char fill;
	cout << "Enter a character for the interior of the box: ";
	cin >> fill;
	return fill;
}

void displayBox (int numRows, int numCols, char fillchar)
{

	int i,j;

	for (i=0;i<numCols;i++)
		cout << "*"; // couts top of box
	cout << endl; // Starts center of box on a new line
	for (j=0; j<numRows-2;j++) // Subtract 2, 1 for top & 1 for bottom
	{	
		cout << "*";// couts left side
		for(i=0;i<numCols-2;i++)// Subtract 2, 1 for each side 
			cout << fillchar;
		cout << "*" << endl; // couts right side and a newline
	}
	for (int i=0; i<numCols; i++)
		cout << '*'; // couts bottom of box
	cout << endl << endl; //  
	return;
}
Topic archived. No new replies allowed.