Floating Point: Stack Underflow

• 10-16-2002
niroopan
Floating Point: Stack Underflow
Code:

```#include <iostream.h>        //cout statement// #include <conio.h>            //getch() and clrscr()// #include <string.h>                //strcpy()// #include <time.h>                        //time function// #include <iomanip.h>                //decimal formatting//                                                                 //function// double mycalc(double x, double& y) {   y = (-2)*x*x + 3*x + 1; }                                                   //main part of program// int main(void) {         double start,end,yvalue;         time_t t=time(NULL);        //time function//                                                                 //Fowler Statement//         cout << setiosflags(ios::right);         cout << setw(79) << ctime(&t)<<endl;         cout << setw(79) << "Gavinn Niroopan"<<endl;         cout << setw(79) << "Mr. Slack"<<endl;         cout << setw(79) << "ICS 3MO-A"<<endl;         gotoxy(28,9);         cout << "Functions and Relations"<<endl;         gotoxy(28,10);         cout << "~~~~~~~~~~~~~~~~~~~~~~~"<<endl;         cout << "Explanation: " <<endl;         cout << "\tUsing variables and looping structures display a chart,the"<<endl;         cout << "\tfollowing assignment will demonstrate a program structure, code"<<endl;         cout << "\tlayout and use logic structures. The program will use data derived"<<endl;         cout << "\tfrom a user and date acquired from the system at time of execution."<<endl;         getch();                        //pause screen//   clrscr();                                //clear screen//   cout << ctime(&t) << endl;   gotoxy(28,3);         cout << "Functions and Relations"<<endl;         gotoxy(28,4);         cout << "~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;   cout << "What is the beginning reference in the range of  (-.2.5 to 3.5) - ";   cin >>start;   cout << "What is the ending reference in the range of  (-.2.5 to 3.5) - ";   cin >> end;   clrscr();   cout << ctime(&t) << endl;   gotoxy(28,3);         cout << "Functions and Relations"<<endl;         gotoxy(28,4);         cout << "~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;         cout << endl;   cout <<setw(30)<< " Value of x " << setw(30) << " y = -2x^2 + 3x + 1 " << endl;   cout <<setw(30)<< " ~~~~~~~~~~ " << setw(30) << " ~~~~~~~~~~~~~~~~~~ " << endl<<endl;   for(start; start<=end; start = start + 0.5)   {           mycalc(start,yvalue);       cout << setiosflags(ios::fixed|ios:: right);       cout << setprecision(1);       cout << setw(26)<< start << setw(26) <<yvalue<<endl;   }   cout << endl;   cout <<setw(45)<< "-Thank You-" << endl;   getch(); }```
I have a problem with the above code. There is a stack underflow error when i input -2.5 to 3.5. Could i get some help please. Thanks :D
• 10-16-2002
bonkey
you are not returning anything.

double mycalc(double x, double& y)

so
void mycalc(double x, double&y)

for(start; start<=end; start = start + 0.5)

you aren't doing anything with start so:

for(; start<=end; start += 0.5)

Those are not causing your problem, just pointing them out.

I think your problem may simply be mathmatical precidence.

y = ((-2)*x*x) + (3*x) + 1; // Use parenthesis to specify the proper precidence. Currently it is:

y= ((-2)*x) * (x +(3 * (X+1)); // Unless this is what you wanted.

It has been a while since I have played with mathematical precidence, so I may be off a little. The point is it is usually best to use the parenthesis to be explicit.

Edit: Changed start = start to start+=