Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stack underflow

Status
Not open for further replies.

RSTR

Programmer
Mar 25, 2000
27
US
I'm creating a program that works fine in long integer.

however, after 16 , the output becomes too big for long integer.

I tried switching to float or double, but during run time I get a stack underflow error.

can someone tell me how to fix this?


rstr@spacemail.com

 
Can you post your code? That would be very helpful. You really didn't give enough information to solve your problem.

As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Sorry, I thought that would happen. (was in a hurry.)
Here goes.....

/* Finds the factorial of a number */

#include <iostream.h>
typedef long int lint;
lint factorial(lint x, lint xdown);
int main()
{
lint x;
lint y;
int choice;
start:
cout << &quot;which number do you want to find the factorial of?: &quot;;
cin >> x;


y = factorial(x, x-1);


cout << &quot;\nthe factorial of &quot; << x << &quot; is &quot; << y;

cout <<&quot;\ndo you want to play again? (0 for no, 1 for yes): &quot;;
cin >> choice;

if(choice == 0)
return 0;
else
goto start;

return 0;
}

lint factorial(lint x, lint xdown)
{
if (xdown < 1)
return x;
else
factorial(x*xdown,xdown - 1);
}


Using function recursion, the program finds the factorial of a number ... (6 * 5 * 4 * 3 ....)

But, after the number 16, the return number becomes too big for long integer, and flips over. When I put in float or double the program ends with &quot;stack underflow&quot; error.

I was wondering if there was a way around this
(is that enough info?) =-) RSTR


rstr@spacemail.com

 
Actually, I'm sorry to have waisted your time.

I tried this on a different compiler, and it worked fine,
although I get an answer that looks like

3.55687e+014, Which I assume is scientific notation.

now my question is, is there a way to get the answer to not look like scientific notation?


rstr@spacemail.com

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top