where is the output

cerebrum's Avatar
Go4Expert Member
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
So what output you expect without giving a cout ??
0
cerebrum's Avatar
Go4Expert Member
#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 !

Last edited by cerebrum; 16Mar2009 at 22:28..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I still dont see getch() ?

Also use Code Blocks for code in posts
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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().
0
cerebrum's Avatar
Go4Expert Member
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.
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Quote:
Originally Posted by cerebrum View Post
#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;
}
0
cerebrum's Avatar
Go4Expert Member
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();

}
*/

Last edited by shabbir; 17Mar2009 at 21:42.. Reason: Code blocks