cplusplus.com
C++ : Forum : General C++ Programming : Void Functions
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


question Void Functions

Pages: 12
thebeast (34)
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;
}
Bazzy (6258)
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.
thebeast (34)
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;
}
firedraco (4744)
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.
jjlimax (2)
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
computerquip (1682)
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.
thebeast (34)
These are the errors I get (x,fout,num and y are) undeclared
Bazzy (6258)
You are using variables scoped in main inside other functions
thebeast (34)
so are you telling me that i should declare everything before??
Bazzy (6258)
You can make your variables global or pass them as arguments
chrisname (4917)
I'd like to add that global variables are evil.
http://www.lenholgate.com/archives/000858.html
thebeast (34)
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;
}
Bazzy (6258)
No. Pass meaningful variables as arguments ( streams as references )
thebeast (34)
hey bazzy can you be just a little more specific pls
Bazzy (6258)
Eg:
1
2
void line(ofstream &fout, char ch, int x);
void rectangle (ofstream &fout, char ch , int x, int y);

thebeast (34)
It is still giving me the same errors
Zhuge (2205)
Did you actually change the code inside your functions to compensate for the changes to the parameters?
thebeast (34)
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;
}
computerquip (1682)
No. using namespace std needs to be before anything except header declerations.
thebeast (34)
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