New to Functions, Help is Appreciated

I have to write a program that prompts the user to enter a size for a box to be displayed (# of rows between 3 and 20, # of columns between 3 and 60, and a character to fill the box with), and then displays a box of the given size, bordered with *'s, and filled with the specified character. I must use the two functions I already have in my code. To help you get a better understanding, if 3,4,"#' are passed as arguments, the function should display:

****
*##*
****

I think I have a good idea of the code, but I cannot get it to compile. I am getting error C2556 that says overloaded function differs only by return type from 'void displayBox(int, int, char)'. Also error message C2371: 'displayBox': redefinition; different basic types. I do not know how to fix this, and any help would be greatly appreciated.


#include <iostream>
using namespace std;

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

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

cout<<"Enter a character for the interior of the box: ";
cin>> chara;

displayBox(numRows, numCols, chara);



return 0;
}

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

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

int displayBox(int numRows, int numCols, char fillChar)
{
for(int i = 0; i < numCols; i++)
{
cout<<'*';
cout<<endl;
}

cout<<'*';

for(int Rowcount = 0; Rowcount < numRows - 2; Rowcount++)
{
for(int j = 0; j < numCols -2; j++)
{
cout<< fillChar;
}
cout<<'*';
cout<<endl;
}

for(int i = 0; i < numCols; i++)
{
cout<<'*';
cout<<endl;
}

return;
}
The problem is that you declared first the function as

void displayBox(int numRows, int numCols, char fillChar);

and then you define the function as

int displayBox(int numRows, int numCols, char fillChar)

You should decide what return type the function will have.

You forgot to add your second cinInRange (int low, int high) function definition.

My mistake, I meant you forgot to call cinInRange () a second time.
Last edited on
This:
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
#include <iostream>
#include <cstdlib>
using namespace std;

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

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

cout<<"Enter a character for the interior of the box: ";
cin>> chara;

displayBox(numRows, numCols, chara);

system ("pause");

return 0;
}

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

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

void displayBox(int numRows, int numCols, char fillChar)
{
for(int i = 0; i < numCols; i++)
{
cout<<'*';
}

cout<<endl<<'*';

for(int Rowcount = 0; Rowcount < numRows - 2; Rowcount++)
{
for(int j = 0; j < numCols -2; j++)
{
cout<< fillChar;
}
cout<<'*';
cout<<endl;
}

for(int i = 0; i < numCols; i++)
{
cout<<'*';
}
return;
}

should work.
Thank you for all of your help, I was able to get it to work.
Topic archived. No new replies allowed.