Void Functions

Pages: 12
closed account (ShUk4iN6)
Hello Everybody.I need to write a C++ program(block letters via functions)

Firstly, write a function
void line( char ch, int num)
which outputs the character ch num times in a row on a given line.

Secondly, write a function

void rectangle (char ch, int x, int y)
which writes the character ch in a rectangular pattern of x rows and y columns.

Thirdly, write a main body to read one character at a time from animals.dat.Th eprogram should produce a rectangular pattern for each letter in the animals name; the size of the rectangular pattern depends on the letter and on its position in the animal name, the rule being: if the x th letter in the name is the y th letter of the alphabet, the rectangle should be size x by y.


This is what i got so far . Thank you for the help



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
#include iostream
#include fstream

void line(char ch, int num);

void rectangle(char ch, int x, int num);

using namespace std;

ifstream fin;
ofstream fout;

void line(char ch, int num)
{
	for(int i=1, i<=x,i++)
	{
	cout<<ch
	fout<<ch
	}
}

cout<<endl;
fout<<endl;

void rectangle (char ch , int, int y)
{
	line(ch,int num);
}
for(int j=1, j=y, j++)
{
	cout<<endl<<endl;
	fout<<endl<<endl;
}

int main()
{
	fin.open("animals.dat");
	int i,j;
	for(int j=1, j=y, j++)
	{
		cout<<endl<<endl;
		cout<<endl<<endl;
	}
		return 0;
}
You are closing the 'rectangle' function brace too early ( Line 28 ). You are not using the x parameter ( nor naming it )
You would need two nested loops there.
closed account (ShUk4iN6)
hmm..ok but I am still getting a lot of errrors.Can you help me?

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
#include <iostream>
#include <fstream>

void line(char ch, int num);

void rectangle(char ch, int x, int num);

using namespace std;

ifstream fin;
ofstream fout;

void line(char ch, int num)
{
	for(int i=1, i<=x,i++)
	{
	cout<<ch
	fout<<ch
	}
}

cout<<endl;
fout<<endl;

void rectangle (char ch , int x, int y)
{
	line(ch,int num);

for(int j=1, j=y, j++)
{
	cout<<endl<<endl;
	fout<<endl<<endl;
}
}

int main()
{
	fin.open("animals.dat");
	int i,j;
	for(int j=1, j=y, j++)
	{
		cout<<endl<<endl;
		cout<<endl<<endl;
	}
		return 0;
}
Your are missing ';' on some of your couts/fouts. You also a cout and and fout floating out in space...you can't do that.
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
#include <iostream>
#include <fstream>

void line(char ch, int num);

void rectangle(char ch, int x, int num);

using namespace std;



void line(char ch, int num)
{
	for(int i=1; i<=x;i++)
	{
		cout<<ch;
		fout<<ch;
	}
}



void rectangle (char ch , int x, int y)
{
	line(ch,num);

	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		fout<<endl<<endl;
	}
}

int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("animals.dat");
	int i,j;
	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		cout<<endl<<endl;
	}
		return 0;
}

and it's still will be have an errors. You should read some tutorials about c++. This web site is perfect for that.
I hope that you don't looking for a guy (loser) whose gonna write that program for you
Last edited on
closed account (S6k9GNh0)
jjlimax, most of the errors you've had so far have been obvious and indicated by the compiler. At least post the errors so we may help and finish this thread.
closed account (ShUk4iN6)
These are the errors I get (x,fout,num and y are) undeclared
You are using variables scoped in main inside other functions
closed account (ShUk4iN6)
so are you telling me that i should declare everything before??
You can make your variables global or pass them as arguments
I'd like to add that global variables are evil.
http://www.lenholgate.com/archives/000858.html
closed account (ShUk4iN6)
so you are telling me that I should do 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>

void line(char ch, int num);

void rectangle(char ch, int x, int num);

using namespace std;

int i,j,x,y;

void line(char ch, int num)
{
	for(int i=1; i<=x;i++)
	{
		cout<<ch;
		fout<<ch;
	}
}



void rectangle (char ch , int x, int y)
{
	line(ch,num);

	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		fout<<endl<<endl;
	}
}

int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("animals.dat");
	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		cout<<endl<<endl;
	}
		return 0;
}
No. Pass meaningful variables as arguments ( streams as references )
closed account (ShUk4iN6)
hey bazzy can you be just a little more specific pls
Eg:
1
2
void line(ofstream &fout, char ch, int x);
void rectangle (ofstream &fout, char ch , int x, int y);

closed account (ShUk4iN6)
It is still giving me the same errors
Did you actually change the code inside your functions to compensate for the changes to the parameters?
closed account (ShUk4iN6)
like 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
#include <iostream>
#include <fstream>

void line(ofstream &fout, char ch, int x);

void rectangle (ofstream &fout, char ch , int x, int y);

using namespace std;



void line(ofstream &fout, char ch, int x)
{
	for(int i=1; i<=x;i++)
	{
		cout<<ch;
		fout<<ch;
	}
}



void rectangle (ofstream &fout, char ch , int x, int y)
{
	line(ch,num);

	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		fout<<endl<<endl;
	}
}

int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("animals.dat");
	int i,j;
	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		cout<<endl<<endl;
	}
		return 0;
}
closed account (S6k9GNh0)
No. using namespace std needs to be before anything except header declerations.
closed account (ShUk4iN6)
this is wat i got so far i have only one error left and it says num is undeclared where am i suppose to delare num in void rectangle??
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
#include <iostream>
#include <fstream>

using namespace std;

void line(ofstream &fout, char ch, int x);

void rectangle (ofstream &fout, char ch , int x, int y);

void line(ofstream &fout, char ch, int x)
{
	for(int i=1; i<=x;i++)
	{
		cout<<ch;
		fout<<ch;
	}
}



void rectangle (ofstream &fout, char ch , int x, int y)
{
	line(ch,num);

	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		fout<<endl<<endl;
	}
}

int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("animals.dat");
	int i,j,y;
	for(int j=1; j=y; j++)
	{
		cout<<endl<<endl;
		cout<<endl<<endl;
	}
		return 0;
}
Pages: 12