isosceles triangle using functions

I'm a beginner in this subject. I'm Trying to add in the function prototype into this program;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//chapter 6 labEx3_1: isosceles triangle using functions
#include <iostream>
using namespace std;
//1. function prototype: printSpaces
//2. function prototype: printStars
int main()
{
 int n;
 cout<<"Enter number of rows: ";
 cin>>n;
 for (int i = 1 ; i <= n ; i ++)
 {
 //3. function invocation: printSpaces
 for (int j=1; j<=n-i; j++) cout << " ";

 //4. function invocation: printStars
 for (int k=1; k<=i*2-1; k++) cout <<"*";
 cout << endl;

 cout<<endl;
 }
 fflush(stdin) ; getchar();  return 0;  }
hmmm, not sure what you're getting at.

Here's how I'd do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

int main() {
	int rows = 0;
	int star = 1;
	cout << "Please enter number of rows." << endl;
	cin >> rows;
		
	for (int i = 0; i < rows; i++){
		for (int k = 0; k < (rows - 1 - i); k++) {
			cout << " ";}
		for (int h = 0; h < (((i - 1) * 2) + 1); h++){
			cout << "*";}
		for (int k = 0; k < (rows - 1 - i); k++) {
			cout << " ";}
		cout << endl;}

	
	return 0;}


Oops, that code contains a bug, here's the edited code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

int main() {
	int rows = 0;
	int star = 1;
	cout << "Please enter number of rows." << endl;
	cin >> rows;
	
	for (int i = 0; i < rows; i++){
		for (int k = 0; k < (rows - i); k++) {
			cout << " ";}
		for (int h = 0; h < (((i - 1) * 2) + 1); h++){
			cout << "*";}
		for (int k = 0; k < (rows - 1 - i); k++) {
			cout << " ";}
		cout << endl;}

	for (int i = 0; i < (((rows - 1) * 2) + 1); i++){
		cout << "*";}

	return 0;}


It may be wrong though.
Last edited on
@OP, does that code work ? Do you only need to put it in functions ? If you have no idea how to write one, see http://www.cplusplus.com/doc/tutorial/functions/
Both functions will take one integer argument and return nothing.
I've included the function prototype but still can't get it to work...

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
#include <iostream>
using namespace std;
void printStars (int stars);
void printSpaces (int spaces);
int main()
{
int n;
cout<<"Enter number of rows: ";
cin>>n;
for (int i = 1 ; i <= n ; i ++) // row main loop

{
printSpaces (n-i);
printStars(i*2-1);
cout<<endl;
}

fflush(stdin); getchar(); return 0; }

void printStars(int stars)
{
for (int j; j=1; j<=stars, j++)
cout <<"*";
}

void printSpaces(int spaces)
{
for (int k; k=1; k<=spaces, k++)
cout <<" ";
}
You messed up your for loops in printStars and printSpaces.
I did this changes and the prototype works. Would there be any other solution to the function prototype?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
void printSpaces();//1. function prototype: printSpaces
void printStars();//2. function prototype: printStars
int main()
{
 int n;
 cout<<"Enter number of rows: ";
 cin>>n;
 for (int i = 1 ; i <= n ; i ++)
 {
 void printSpaces();//3. function invocation: printSpaces
 for (int j=1; j<=n-i; j++) cout << " ";

 void printStars();//4. function invocation: printStars
 for (int k=1; k<=i*2-1; k++) cout <<"*";
 cout << endl;

 cout<<endl;
 }
 fflush(stdin) ; getchar();  return 0;  }
No. There are no functions in this code. Lines 12, 16 only declare the functions, not invoke them. The code in your previous post was good, except for the for loops.
closed account (DSLq5Di1)
Function Prototypes 101

Why do we need a function prototype/declaration?
Short answer, you don't, but compilers like humans, will read your program from top to bottom. Take for example,

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void sayHello() // Here, we are defining what sayHello() does. This is a function definition.
{
    std::cout << "Hello!";
}

int main()
{
    sayHello(); // A function call. The compiler knows that sayHello() exists above us.
    return 0;
}

No problems there, but what if sayHello() was defined after main?
Identifier not found.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    sayHello(); // sayHello()? What the fudge?? The compiler has not encountered our function yet.. 
                // it doesnt know what it is, what arguments it takes or what it may return.
    return 0;
}

void sayHello() // Feeling neglected.
{
    std::cout << "Hello!";
}

So what do we do? should we rearrange our program so that all functions are defined before being called?
That is an option for small projects, but becomes an unsightly mess on anything larger. This is where function declarations come in,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void sayHello(); // This is a function declaration. We have not defined what it does!
                 // Simply that it exists, has no arguments and will return nothing.
int main()
{
    sayHello(); // sayHello()? Yes I've heard of that function, right away sir!
    return 0;
}

void sayHello() // What if this definition did not match your declaration? or even exist?
                // sayHello() in main would produce a linker error, an unresolved external symbol.
{
    std::cout << "Hello!";
}

At the very least the compiler needs to know, the name, arguments and return value of a function.. this is its signature and must remain identical to your definition.

ps. Sorry for the long blurb!

Last edited on
Hi hamsterman, you've mentioned that i've messed up the loop... May i ask in which line and which part that i messed up??
for (int k; k=1; k<=spaces, k++)
is wrong,
for (int k=1; k<=spaces; k++)
is right.

Those are lines 22, 28 in http://www.cplusplus.com/forum/beginner/47134/#msg256621
Ok.. I did change the int but it still doesn't work..
I've modified to call out the stars and spaces after the return 0.. But nothing appear...
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
#include <iostream>
using namespace std;
void printSpaces();//1. function prototype: printSpaces
void printStars();//2. function prototype: printStars
int main()
{
 int n;
 cout<<"Enter number of rows: ";
 cin>>n;
 for (int i = 1 ; i <= n ; i ++)
	 {
 void printSpaces();//3. function invocation: printSpaces
 for (int j=1; j<=n-i; j++)

 void printStars();//4. function invocation: printStars
 for (int k=1; k<=i*2-1; k++) 
 cout << endl;
 }
 
fflush(stdin); getchar(); return 0;
 }
void printSpaces()
{
cout <<" ";
}
void printStars()
{
cout <<"*";
}
I wrote:
No. <...>Lines 12, 16 only declare the functions, not invoke them. The code in your previous post was good, except for the for loops.
It's 12, 15 this time.

void func();
is a declaration
func();
is a call. Do you see a difference ?
Ok got it... 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Student Name : Muhammad A'bid Bin Mohamed Albakri
// ID: P1152320
// Class: EDME/EO/2B/23
//chapter 6 labEx3_1: isosceles triangle using functions
#include <iostream>
using namespace std;
void printStars(int stars);
void printSpaces(int spcs);
int main()
{
	int n;
	cout<<"Enter number of rows: ";
	cin>>n;
	for (int i = 1 ; i <= n ; i ++)  //row main loop
	{
	        printSpaces(n-i);
			printStars(i*2-1);
		    cout<<endl;
	}
	fflush(stdin) ; getchar(); 	return 0;		}


void printStars(int stars)
{
   int j;
   for (j=1; j<=stars; j++) 
	   cout << "*"; 
}

void printSpaces(int spcs)
{
	int j;
	for (j=1; j<=spcs; j++)
		cout <<" ";
}
/* Output
Enter number of rows: 9
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
*/

*/
Topic archived. No new replies allowed.