Thread: Wrong Output!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    Wrong Output!

    Code:
    #include<cstdlib>
    #include<iostream>
    
    using namespace std;
    
    int factorial(int *,int);  //number and result
    int main()
    {
        int fact,num;
        cout<<"Enter a number!"<<endl;
        cin>>num;
        fact=factorial(&num,1);
        cout<<"fact is : "<<fact<<endl;
        system("PAUSE");
        return 0;
    }
    int factorial(int *n,int r)
    {
        if(*n<=1)
           return r;
        else
        {
            r = r * (*n);
            *n-=1;
            cout<<"r is : "<<r<<endl;
            factorial(n,r);
        }   
    }
    This is a factorial program everything works fine, but the output of fact in main is way off and the variable returning to fact in function factorial is giving the right output.

    This is the second time this happened, so any ideas?

    I'm using Dev-cpp, just for the record!

    Thank You!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are not returning the value when you are recursing inside factorial.

    I also see absolutely no point in passing n as a pointer, or passing r around.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you put warnings to max, don't you get a warning similar to "not all control paths return a value"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    what? if I return the value during recursion the control shifts back to main and i get the wrong output

    how do i put warnings to max ?

    I have

    1)enable exceptional handling
    2) generate debugging info

    set to yes in the compiler options

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kolliash View Post
    what? if I return the value during recursion the control shifts back to main and i get the wrong output
    No you won't, and if you do, the code itself is flawed.
    The thing is that if n is NOT <= 1, the function will return NOTHING, ie undefined return.
    So the entire result then becomes undefined.

    how do i put warnings to max ?
    Typically find options with the command line -wall and maybe pendantic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Code:
    #include<cstdlib>
    #include<iostream>
    
    using namespace std;
    
    int factorial(int *,int);  //number and result
    int main()
    {
        int fact,num;
        cout<<"Enter a number!"<<endl;
        cin>>num;
        fact=factorial(&num,1);
        cout<<"fact is : "<<fact<<endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    int factorial(int *n,int r)
    {
        if((*n<=1))
           return r;
        else
        {
            
            r = r * (*n);
            (*n)-=1;
        }
        r=factorial(n,r);
        return r;
    }
    It works!!

    Thank You matsp,Elysia

    since the return type is int, i forgot to return the value to r for every call.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good. Now simplify your code to work with:
    Code:
    int factorial(int n);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong with the Output?
    By shabbirhussain in forum C Programming
    Replies: 5
    Last Post: 08-26-2008, 08:28 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM