Wrong Output!

This is a discussion on Wrong Output! within the C++ Programming forums, part of the General Programming Boards category; 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; ...

  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
    Posts
    23,031
    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
    Posts
    23,031
    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
    22,308
    Good. Now simplify your code to work with:
    Code:
    int factorial(int n);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:28 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 11: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, 12:56 PM

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