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. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    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. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Do you know what the second row in your for-loop does?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Yes...calculate and get the factorial number and I meant the sum of the number.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by kenryuakuma View Post
    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;
               
            }
    }
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    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. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Why is it called "return" ?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Quote Originally Posted by Salem View Post
    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. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    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. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kenryuakuma View Post
    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. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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. #13
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by CornedBee View Post
    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
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21