Thread: recursive factorial prblm

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    recursive factorial prblm

    Could anyone help me debug this

    // recursive factorial prblm 4 pg 290 c++ primer plus

    #include <iostream>
    using namespace std;

    float factorial(int n);

    int main()
    {
    int n;
    float fact;

    cout << "Enter an integer:";
    cin >> n;

    fact=factorial(n);

    cout << "The factorial is:" << fact << endl;

    }

    float factorial(int n)
    {
    float temp
    if (n>0)
    temp= n*factorial(n-1);

    return temp;
    }

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    hmmm looks ok what errors are you getting.
    -

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    what about the 'return 0' in main.. is that the error

    and the ; for float temp
    -

  4. #4
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    float factorial(int n)
    {
    float temp
    if (n>0)
    temp= n*factorial(n-1);

    No ; after float temp
    Visit entropysink.com - It's what your PC is made for!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Does no one read the error messages? It seems like people stop reading after they see 'Error' and come ask us to debug it without even trying. You had a syntax error and a warning about main returning a value.
    Code:
    #include <iostream> 
    using namespace std; 
    
    float factorial(int n); 
    
    int main() 
    { 
      int n; 
      float fact; 
      cout << "Enter an integer:"; 
      cin >> n; 
      fact=factorial(n); 
      cout << "The factorial is:" << fact << endl; 
      return 0;
    } 
    
    float factorial(int n) 
    { 
      float temp; 
      if (n>0) 
        temp= n*factorial(n-1); 
      return temp; 
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    thanks for all the help, being a newbe i didnt spot the semi colons though i stared at my source code wondering what was wrong. From now on thats the first thing ill check

    thanks again

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Not to be a little hasty, but doesn't your compiler give you any clues on what is wrong?

    I agree with Prelude, a simple once over the error messages and even a run through with a debugger (something that nobody here uses for some reason) will solve 85% of the problems posted on the board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP me Factorial Recursive
    By helpme1234 in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2006, 10:51 PM
  2. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  3. Recursive factorial, what is wrong with my code?
    By xephyr in forum C Programming
    Replies: 10
    Last Post: 07-25-2004, 09:09 AM
  4. recursive factorial function
    By brianptodd in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2003, 12:56 AM