# Void Functions

• (2 Pages)
• 1
• 2

## 15 Replies - 888 Views - Last Post: 10 November 2009 - 05:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137928&amp;s=396996a0cf8e0e4b6ca029bb12a5100d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 thebeast91

Reputation: 1
• Posts: 63
• Joined: 26-October 09

# Void Functions

Posted 09 November 2009 - 06:58 PM

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

```#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;
}
```

Is This A Good Question/Topic? 0

## Replies To: Void Functions

### #2 jbord39

Reputation: 4
• Posts: 13
• Joined: 09-October 09

## Re: Void Functions

Posted 09 November 2009 - 08:38 PM

Quote

```

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;
}
}

```

You did not ever define num.

### #3 thebeast91

Reputation: 1
• Posts: 63
• Joined: 26-October 09

## Re: Void Functions

Posted 09 November 2009 - 09:30 PM

jbord39, on 9 Nov, 2009 - 07:38 PM, said:

Quote

```

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;
}
}

```

ok tthis is what i did and now it tells me that line function doesnt take 2 arguments
```#include <iostream>
#include <fstream>

using namespace std;
int num;

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;
}
```

You did not ever define num.

### #4 no2pencil

• Original Digital Gansta

Reputation: 4464
• Posts: 24,909
• Joined: 10-May 07

## Re: Void Functions

Posted 09 November 2009 - 09:39 PM

thebeast91, on 9 Nov, 2009 - 10:30 PM, said:

ok tthis is what i did and now it tells me that line function doesnt take 2 arguments

You define line to take three arguments :

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

But then you only pass two :

```line(ch,num);
```

Either declare it to only take two, or when you call it pass three.

### #5 thebeast91

Reputation: 1
• Posts: 63
• Joined: 26-October 09

## Re: Void Functions

Posted 09 November 2009 - 10:00 PM

no2pencil, on 9 Nov, 2009 - 08:39 PM, said:

thebeast91, on 9 Nov, 2009 - 10:30 PM, said:

ok tthis is what i did and now it tells me that line function doesnt take 2 arguments

You define line to take three arguments :

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

But then you only pass two :

```line(ch,num);
```

Either declare it to only take two, or when you call it pass three.

ok when i do that it tells me that i didnt declare fout
this is what you mean right??
```#include <iostream>
#include <fstream>

using namespace std;
int num;

void line(char ch, int x);

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

void line(char ch, int x)
{
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,y;
for(int j=1; j=y; j++)
{
cout<<endl<<endl;
cout<<endl<<endl;
}
return 0;
}
```

### #6 Oler1s

• D.I.C Lover

Reputation: 1394
• Posts: 3,884
• Joined: 04-June 09

## Re: Void Functions

Posted 09 November 2009 - 10:37 PM

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

```

Indicate what line of code defines fout in this function. Where is it?

### #7 jbord39

Reputation: 4
• Posts: 13
• Joined: 09-October 09

## Re: Void Functions

Posted 09 November 2009 - 11:00 PM

Oler1s, on 9 Nov, 2009 - 09:37 PM, said:

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

```

Indicate what line of code defines fout in this function. Where is it?

I'm going to guess it's the line that contains fout. It's because you did not pass fout as a parameter to this function.
The syntax for this would be:

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

The & denotes a call by reference (a pointer to the original output stream).

When writing functions, remember these things:

1. streams are nearly always called with the & operator.
2. make sure that when you declare the function prototype, define the function, and call the function there are the same number of parameters for which the parameters match each other. ex:
``` //prototype
void line(int x, char toBePrinted, ofstream& fout);
```

Then in main this would be called something like this:

```int main()
{
int number;
char toBePrinted;

ofstream& fout;
ofstream.open("output.dat");
if(ofstream.fail())
{
cout << "Problem with output file\n";
exit(1);
}

cout << "Please enter the character to be printed, then the number of times for it to print\n";
cin >> number;
cin >> toBePrinted;
line(toBePrinted, number, fout);
}

```

And then defined underneath main it would look like this:

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

This post has been edited by JackOfAllTrades: 10 November 2009 - 05:43 AM

### #8 thebeast91

Reputation: 1
• Posts: 63
• Joined: 26-October 09

## Re: Void Functions

Posted 09 November 2009 - 11:13 PM

jbord39, on 9 Nov, 2009 - 10:00 PM, said:

Oler1s, on 9 Nov, 2009 - 09:37 PM, said:

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

```

Indicate what line of code defines fout in this function. Where is it?

I'm going to guess it's the line that contains fout. It's because you did not pass fout as a parameter to this function.
The syntax for this would be:

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

The & denotes a call by reference (a pointer to the original output stream).

When writing functions, remember these things:

1. streams are nearly always called with the & operator.
2. make sure that when you declare the function prototype, define the function, and call the function there are the same number of parameters for which the parameters match each other. ex:
//prototype
void line(int x, char toBePrinted, ofstream& fout);

Then in main this would be called something like this:

int main()
{
int number;
char toBePrinted;

ofstream& fout;
ofstream.open("output.dat");
if(ofstream.fail())
{
cout << "Problem with output file\n";
exit(1);
}

cout << "Please enter the character to be printed, then the number of times for it to print\n";
cin >> number;
cin >> toBePrinted;
line(toBePrinted, number, fout);
}

And then defined underneath main it would look like this:

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

something like this??

```#include <iostream>
#include <fstream>

using namespace std;
int num;

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

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

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

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

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

int main()
{
int number;
char ch;

ofstream& fout;
ofstream.open("animals.dat");
if(ofstream.fail())
{
cout << "Problem with output file\n";
exit(1);
}

cout << "Please enter the character to be printed, then the number of times for it to print\n";
cin >> number;
cin >> ch;
line(ch, num, fout);
}

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

### #9 jbord39

Reputation: 4
• Posts: 13
• Joined: 09-October 09

## Re: Void Functions

Posted 09 November 2009 - 11:22 PM

```#include <iostream>
#include <fstream>

using namespace std;

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

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

int main()
{
int number;
char ch;

ofstream fout;
fout.open("animals.dat");
if(fout.fail())
{
cout << "Problem with output file\n";
exit(1);
}

cout << "Please enter the character to be printed\n";
cin >> ch;
cout << "Please enter the number of times to be printed\n";
cin >> number;
line(ch, number, fout);
}

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

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

line(ch, x, fout);

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

```

No like this . You declared mixed up ofstream and fout a lot. You should read up about when to use the & because it is extremely helpful for any function that you need to return two values. You also declared line function twice, and you declared num as a global variable (makes no sense) and you never set it equal to anything. This code is not complete but the line function works; although you will have to test the square on your own.

Hope this helps,

John

### #10 jjl

• Engineer

Reputation: 869
• Posts: 3,994
• Joined: 09-June 09

## Re: Void Functions

Posted 09 November 2009 - 11:24 PM

you need to return 0; after main(),

### #11 thebeast91

Reputation: 1
• Posts: 63
• Joined: 26-October 09

## Re: Void Functions

Posted 09 November 2009 - 11:27 PM

jbord39, on 9 Nov, 2009 - 10:22 PM, said:

```#include <iostream>
#include <fstream>

using namespace std;

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

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

int main()
{
int number;
char ch;

ofstream fout;
fout.open("animals.dat");
if(fout.fail())
{
cout << "Problem with output file\n";
exit(1);
}

cout << "Please enter the character to be printed\n";
cin >> ch;
cout << "Please enter the number of times to be printed\n";
cin >> number;
line(ch, number, fout);
}

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

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

line(ch, x, fout);

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

```

No like this . You declared mixed up ofstream and fout a lot. You should read up about when to use the & because it is extremely helpful for any function that you need to return two values. You also declared line function twice, and you declared num as a global variable (makes no sense) and you never set it equal to anything. This code is not complete but the line function works; although you will have to test the square on your own.

Hope this helps,

John

everyone telling me other stuff so everyone confused me but thank you very much

### #12 jbord39

Reputation: 4
• Posts: 13
• Joined: 09-October 09

## Re: Void Functions

Posted 09 November 2009 - 11:28 PM

ImaSexy, on 9 Nov, 2009 - 10:24 PM, said:

you need to return 0; after main(),

Dude I fixed the program in like 2 minutes don't lecture me. And return 0 isn't necessary in main.

And the program that I just gave you works. Just copy it into your compiler man.

This post has been edited by jbord39: 09 November 2009 - 11:32 PM

### #13 thebeast91

Reputation: 1
• Posts: 63
• Joined: 26-October 09

## Re: Void Functions

Posted 09 November 2009 - 11:38 PM

jbord39, on 9 Nov, 2009 - 10:28 PM, said:

ImaSexy, on 9 Nov, 2009 - 10:24 PM, said:

you need to return 0; after main(),

Dude I fixed the program in like 2 minutes don't lecture me. And return 0 isn't necessary in main.

And the program that I just gave you works. Just copy it into your compiler man.

ok dude I am cool with it thank you again

### #14 jjl

• Engineer

Reputation: 869
• Posts: 3,994
• Joined: 09-June 09

## Re: Void Functions

Posted 09 November 2009 - 11:38 PM

Cool it buddy, and yes you must return 0; its required for c++ standards.
Do I need to copy and paste the standards for you? Or are you all about bad practice? Just because your compiler doesnt screem at you doesnt mean the code is correct. Main still returns a value, without declaring it your just getting garbage returns

This post has been edited by ImaSexy: 09 November 2009 - 11:40 PM

### #15 Oler1s

• D.I.C Lover

Reputation: 1394
• Posts: 3,884
• Joined: 04-June 09

## Re: Void Functions

Posted 10 November 2009 - 01:12 AM

ImaSexy said:

Cool it buddy, and yes you must return 0; its required for c++ standards.
Actually, the OP is correct here. main is a special exception. There is an implicit return 0, so you don't need to explicitly do so.

Quote

Do I need to copy and paste the standards for you?
Ironically, you should do that.

jbord39 said:

Dude I fixed the program in like 2 minutes don't lecture me.
You post your code here, you get criticism. You make statements here, and they can invite debate. If you want to show that someone is incorrect, do it with logic and reason.

This post has been edited by Oler1s: 10 November 2009 - 01:13 AM