Thread: C++ help

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    10

    C++ help

    Code:
    int main()
    { int num, factorial;
       num = 6;
       factorial = Fact(num);
       cout << num << "! = " 
               << factorial;
       return 0;
    } // end main
    int Fact (int num)
    {  if (num == 0)
          return 1;
       else
          return num*Fact(num-1);
    } //end Fact
    I cant get this to to compile. What is supposed to be the output of this?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Put the main() function at the bottom.
    You also forgot to:
    Code:
    #include <iostream>
    using namespace std;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    10
    Code:
    #include <iostream>
    using namespace std;
    
    
    
       int num, factorial;
       num = 6;
       factorial = Fact(num);
       cout << num << "! = " 
               << factorial;
       return 0;
    } // end main
    int Fact (int num)
    {  if (num == 0)
          return 1;
       else
          return num*Fact(num-1);
    } //end Fact
     int main()
    This?

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Not even close:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int Fact (int num)
    {  if (num == 0)
          return 1;
       else
          return num*Fact(num-1);
    } //end Fact
    
    
    int main()
    { int num, factorial;
       num = 6;
       factorial = Fact(num);
       cout << num << "! = " 
               << factorial;
       return 0;
    } // end main
    It outputs "6! = 720" for whatever good that is...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    10
    thanks alot

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Yeah but thats the last freebie.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed