# Curly brace question

This is a discussion on Curly brace question within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int triangle(int); int main() { using namespace std; // Declare a variable as fact_num that represent factorial ...

1. ## Curly brace question

Code:
```#include <iostream>

int triangle(int);

int main()
{
using namespace std;

// Declare a variable as fact_num that represent factorial number
int fact_num;

// Promt user for input
cout << "Please enter a number: ";
cin >> fact_num;
cout << "Your factorial number is: " << triangle(fact_num) << endl;
system("pause");
return 0;
}

// The function body. That's the function definition
int triangle(int fact_num){
int n;
int sum = 0;
for(n = 1; n <= fact_num; n++){
sum += n;
return sum;
}
}```
Well...This is not a working programming because of the additional curly brace of the for loop. Hence, the test for the factorial doesn't work. But without the curly brace, it works fine. My question is why? How come it doesn't with the curly brace included for the for loop?

2. Do you know what the second row in your for-loop does?

3. Yes...calculate and get the factorial number and I meant the sum of the number.

4. Originally Posted by kenryuakuma
Code:
```    // The function body. That's the function definition
int triangle(int fact_num){
int n;
int sum = 0;
for(n = 1; n <= fact_num; n++){
sum += n;
return sum;
}
}```
you failed to add the proper closing brace for teh for loop, try this -

Code:
```    // The function body. That's the function definition
int triangle(int fact_num){
int n;
int sum = 0;
for(n = 1; n <= fact_num; n++) sum += n;

return sum;

}
}```

5. Abachler in you case, doesn't the code miss one curly brace?

Code:
```  // The function body. That's the function definition
int triangle(int fact_num){
int n;
int sum = 0;
for(n = 1; n <= fact_num; n++) sum += n;

return sum;

}
}```

6. First, fix the indentation of triangle. This will make things a lot easier, because it won't confuse abachler.

Next, your explanation of the second line of the for loop was wrong:
Code:
`return sum;`
Try again to explain what this line does.

7. If you're still having trouble with your loop, then use the debugger to single-step the code.
Then you'll see what we're all carping on about.

8. Why is it called "return" ?

9. Originally Posted by Salem
If you're still having trouble with your loop, then use the debugger to single-step the code.
Then you'll see what we're all carping on about.
The debugger is your best friend.
And what IDE are you using that was able to mess up indentation that much.

10. Code:
```#include <iostream>
using namespace std;

// Function must be declared before being used.
int triangle(int);

int main(){
// Declare a variable as fact_num that represent factorial number
int fact_num;

// Promt user for input
cout << "Please enter a number: ";
cin >> fact_num;
cout << "Your factorial number is: " << triangle(fact_num) << endl;
system("pause");
return 0;
}

// Factorial-number function
// Return 1 + 2 + ..... + n
// Factorial number is
// triangle(5) = 1 + 2 + 3 + 4 + 5 = 15
int triangle(int fact_num){
int n;
int sum = 0;
for(n = 1; n <= fact_num; n++){
sum += n;          // this one is the same as sum = sum + n
return sum;
}
}```
Well this is what factorial number is...And I have to do the testing. The question is like how come the compiler generated errors by putting an extra pair of curly braces in the for-loop. Yes...I am a damn noob, I don't even understand what the compiler really says. Even if I do, I just know every little about it.

11. Originally Posted by kenryuakuma
The question is like how come the compiler generated errors by putting an extra pair of curly braces in the for-loop.
Because it didn't. You did, as we've been trying (and failing) to explain. Remember, the machine executes the program you wrote, not the program you wish you wrote. Also remember that everything instantly stops when you hit return, even in the middle of a loop.

12. When you "return" from a function, the function closes and continues the main program, so for the first time the for loop executes it returns just the result of the first loop. I dont know if you understood, but your triangle function should look as this:
Code:
```    int triangle(int fact_num){
int n;
int sum = 0;
for(n = 1; n <= fact_num; n++)
sum += n;

return sum;
}```

13. Originally Posted by CornedBee
First, fix the indentation of triangle. This will make things a lot easier, because it won't confuse abachler.
lol, yeah i missed that one