Thread: Need help w/ debugging errors

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Need help w/ debugging errors

    I'm trying to write a program to give the factorial of a number from the user. If you've read the www.cprogramming.com tutorial on recursion then you'll know why. The problem is that the compiler I'm using (Dev-C++) is giving errors for code I think is right... (in other words its wrong :P)

    Heres the code:
    Code:
    #include <iostream.h>
    
    //Generate factorial numbers
    
    int yournum;
    int final;
    int working;
        
    int main()
        {
        cin>>yournum;
        final=yournum;
        working=yournum-1;
        if(yournum<=0)
            {
            cout<<"Error sorry";
            }
            else
            {
            factorial();
            }
        cout<<"\nPress any key to exit";
        cin.get();
        return 0;
        }
    
    void factorial()
        {
        if(working<=0)
            {
            cout<<"Your number was !", yournum, " which equals ", final;
            return;
            }
        working=working-1;
        working*final=final;
        factorial();
        }
    (sorry if its too big but its not as if its a huge program...)

    There errors are line 21 which is "factorial();" and line 36 which is also "factorial();".
    What am I doing wrong?? :S

    Thanks....

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    #include <iostream>  // Note, its not iostream.h
    include namespace std; // When changing iostream.h to iostream include this, otherwise you will get errors.
    
    // Function prototype:
    void factorial();
    
    //Generate factorial numbers
    
    int yournum;
    int final;
    int working;
        
    int main()
    {
        cin>>yournum;
        final=yournum;
        working=yournum-1;
        if(yournum<=0)
            {
            cout<<"Error sorry";
            }
        else
            {
            factorial();
            }
        cout<<"\nPress any key to exit";
        cin.get();
        return 0;
    }
    
    void factorial()
    {
        if(working<=0)
            {
            cout<<"Your number was !", yournum, " which equals ", final;
            return;
            }
        working=working-1;
        working*final=final;
        factorial();
    }

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sneaky little linker errors...
    By Tozar in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2006, 05:40 AM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Program Fatal Error- Please Explain
    By luckygold6 in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2003, 08:56 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM