//chapter 6 labEx3_1: isosceles triangle using functions
#include <iostream>
usingnamespace 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; }
#include<iostream>
usingnamespace 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:
#include<iostream>
usingnamespace 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;}
@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.
#include <iostream>
usingnamespace 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.
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.