Having trouble making a traingle using void functions

Here is what is required:

Create a function that when executed displays a line of asterisks across the screen. The function will have one parameter that is used to determine the number of asterisks to print. The functions prototype will be:

void drawBar(int);

Include a function used to test the drawBar function called printTriangle. This function will accept one argument that will be used to determine the number of asterisks used to form the base of the triangle. The user should be prompted to enter the base-size of the triangle. The prototype of the printTriangle will be:

void printTriangle(int);

When ran, the program will generate the following output:

Enter the triangle base size: 6

*
**
***
****
*****
******

I guess i really must not understand the void function because I can not seem to get this to work currectly! Any help would be great

#include <iostream>
using namespace std;

void printTriangle(int);
void drawBar(int);

int main()
{
int base = 0;
cout << "This program draws a Triangle" << endl;
cout << "\nEnter the triangle base size: ";
cin >> base;
printTriangle(base);
return 0;
}
void printTriangle(int bs)
{
drawBar(bs);
for (int i = 0; i <= (bs - i); i++);

}
void drawBar(int b)
{
for (int i = 0; i <= (b - i); i++);
cout << "*";
cout << endl;
}
Please use [code][/code] around your code.

Don't do i <= (b - i)

That's the equivalent of:

i <= b - i
+i ...... +i (add i to each side)
2i <= b

That's not what you want it to do.

Also in printTriangle(), you're for loop does nothing, and shouldn't have a semicolon after it. Do something like

1
2
3
4
for(int i = 1; i <= bs; ++i)
{
     drawBar(bs);
}
Last edited on
Try this. And yes, difficult to read code without the [c0de] [c0de] tag. I just basically fix your for loop.

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
#include <iostream>
using namespace std;

void printTriangle(int);
void drawBar(int);

int main() {
int base(0);
cout << "This program draws a Triangle" << endl;
cout << "\nEnter the triangle base size: ";
cin >> base;
printTriangle(base);
}//main

void printTriangle(int bs){
	for(int i = 0; i <= bs; i++){
	drawBar(i);
	cout << endl; 
	}//for
}//printTriangle

void drawBar(int b){
	for(int i = 0; i < b; i++){
	cout << "*"; 
	}//for
}//drawbar  
Last edited on
Thank you very much. I will remember next time about the [c0de][/c0de]
can't believe it was that small of an error. Thanks for pointing it out.
Last edited on
Topic archived. No new replies allowed.