Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   where is the output (http://www.go4expert.com/forums/output-t16558/)

cerebrum 16Mar2009 21:55

where is the output
 
Code:

#include<iostream>
#include<conio.h>

using namespace std;

int fac(int n)
{
  return n*fac(n-1);
}
   

int main()
{
    fac(3);
}

running the following program in bloodshed dev C++, the output just flashes for a milisecond . tried getch() and all other possible stuff.

shabbir 16Mar2009 22:15

Re: where is the output
 
So what output you expect without giving a cout ??

cerebrum 16Mar2009 22:25

Re: where is the output
 
#include<iostream>
#include<conio.h>

using namespace std;

int fac(int n)
{
int c;
c = n*fac(n-1);
cout<<c;
}


int main()
{
fac(3);
}


tried this also ! tried with getch( ) also !

shabbir 16Mar2009 22:47

Re: where is the output
 
I still dont see getch() ?

Also use Code Blocks for code in posts

xpi0t0s 16Mar2009 22:58

Re: where is the output
 
If you open a DOS box in the directory containing the executable and run the executable from the command line then you will see the output.

The problem is that when you run a DOS application in Windows, as soon as the executable finishes, Windows closes the DOS box so you don't get to see the output. Visual Studio sort of acknowledges this by adding code to prompt for a keypress after your main function has returned, so that's what you'll need to do, and that's what shabbir's on about when he mentions getch().

cerebrum 17Mar2009 08:48

Re: where is the output
 
right, dats y i said in my previous message that i tried getch( ) in main( ) , sorry for not mentioning it in the code.

I wish if u cud rewrite the code for me so that i cud figure out the problem whether it is in the code or with the compiler.

asadullah.ansari 17Mar2009 10:06

Re: where is the output
 
Quote:

Originally Posted by cerebrum (Post 44428)
#include<iostream>
#include<conio.h>

using namespace std;

int fac(int n)
{
int c;
c = n*fac(n-1);
cout<<c;
}


int main()
{
fac(3);
}


tried this also ! tried with getch( ) also !



for any recursion function, termination condition should be.
your code should be like..
Code:

#include<iostream>
#include<conio.h>

using namespace std;

int fac(int n)
{
int c;
if(n<=1)
 return 1;
else
 return(n*fac(n-1));
 
}


int main()
{
  int factVal=0;
  factVal = fac(3);
  cout<<"Fatorial of 3 is "<<factVal<<endl;
  getch();
  return 0;
}


cerebrum 17Mar2009 21:10

Re: where is the output
 
Ah, It was more a logical error ! I should have considered that i was dealing with recursion.
thanks.

wipe out the variable c in your code.

the correct code close to my earlier one should be like this :

/*
Code:

#include<iostream>
#include<conio.h>

using namespace std;

int fac(int n)
{
int c;
if(n<=1)
 return 1;
else
return c = n*fac(n-1);

}


int main()
{

cout<<fac(4);
getch();

}

*/


All times are GMT +5.5. The time now is 00:00.