Problem understanding how void functions work

Hello, so for my assignment, I am supposed to write a program that will display a rectangular design, rows by columns, using a character (char) that a user enters. I have to ask the user for the dimension of the rectangle, the character to be used, and send the info to a function called rectangle(), so it can display the rectangular design. I really don't understand how void works out, I have written out most of the program and I need some guidance figuring out what I'm doing wrong, it would be great if someone can explain that to me, thanks!

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
//Program that will display a rectangular design
#include<iostream>
using namespace std;

void rectangle();

{
	int row,column;
	for(row=1;row<=5;row++)
	{
		for(column=1;column<=5;column++)
		{
			cout<<"$ ";
			cout<<endl;
		}
	}
}
	void rectangle(int l,int w,char c)
{
	cout<<"Enter the number of rows and columns of the rectangle"<<endl;
	cin>>l>>w;
	cout<<"Now enter the character to be used to make the rectangle:"<<endl;
	cin>>c;
	cout<<"Here is your rectangle:"<<endl;
	cin>>
}

I think you want something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Program that will display a rectangular design
#include<iostream>
using namespace std;


	void rectangle(int l,int w,char c)
{

cout << l << " "  << w << " "  << c << endl; 
}

int main ()
{
int l=0, w=0;
char c;
	cout<<"Enter the number of rows and columns of the rectangle"<<endl;
	cin>>l>>w;
	cout<<"Now enter the character to be used to make the rectangle:"<<endl;
	cin>>c;
rectangle (l,w,c);

}


what the void function doesn't do.. is output or return any value.

If you look at SamuelAdams code and took out line 20, you wouldn't output the dimensions entered by the user. you should try out his program comment out line 20 then run it and see what happens. Then remove the comment lines from line 20 and run it again.
Last edited on
Topic archived. No new replies allowed.