For Loop in C++

This is a discussion on For Loop in C++ within the C++ Programming forums, part of the General Programming Boards category; dear, i am a novice of c++. the below program compiles perfectly and works as expected. However, I need to ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    For Loop in C++

    dear,

    i am a novice of c++. the below program compiles perfectly and works as expected. However, I need to understand why the 'for loop' did not print the factorial line as many times as the loop.

    Code:
    // program to computer factorial
    
    #include <iostream>
    
    void main()
    {
        unsigned int numb;       // input variable
        unsigned long fact=1;  // the factorial variable
        
        cout << "\nenter the number: ";
        cin >> numb;
        
        for(int j=numb; j>0; j--)        // initialising the loop
         fact *= j;
        cout << "Factorial is " << fact; // result is factorial
        
    }
    I am referring to Turbo C++ by Robert Lafore . In the previous example the same for loop was used to print several lines. I could not differentiate why the cout << did not print many lines.

    can you please guide, thanks

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you have the cout line outside the loop

    when you want to do multiple lines in a loop, you need curly brackets around the code block.

    ie:
    Code:
    for(........)
    {
       instruction 1
       instruction 2
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    4
    dear fib,

    many thanks for your quick reply. i am using bloodshed ide in winxp os. i tried the curly braces already it does not work. can you please have a relook. thanks.


    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        unsigned int numb;       // input variable
        unsigned long fact=1;  // the factorial variable
        
        cout << "\nenter the number: ";
        cin >> numb;
        
        for(int j=numb; j>0; j--)        // initialising the loop
            fact *= j;
                {
                cout << "Factorial is " << fact; // result is factorial
                }                
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    i have deleted the windows support stuff in my original post. now i have included them too to be doubly sure that the dos-in-windows support is not the cause.

    best regards

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    put the opening brace before the fact *= j; The code block is intended to start right after the for statement.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    4
    dear fib,

    it works. tks.

    best regards

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > void main()
    main returns an int
    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.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ha! Salem, do you just search the board for instances of void main? I'm no psychiatrist but that sounds like a sickness to me.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

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