floating point stack underflow

This is a discussion on floating point stack underflow within the C++ Programming forums, part of the General Programming Boards category; When I run the program I get a floating point stack underflow error. I have searched but I can't seem ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    floating point stack underflow

    When I run the program I get a floating point stack underflow error.
    I have searched but I can't seem to seem to find the solution.
    Any other errors are not my worry now just the stack underflow.

    Code:
    /* This program will allow the user to display the formula for calculating
    	the area of a figure.
    	r = rectangle
    	t = triangle
    	c = circle
    	p = parallelogram
    */
    
    ///////////////////////////////////////////////////////////////////////////
    
    #include <iostream.h>
    #include <conio.h>
    #define PI 3.141592
    
    // Function prototype
    
    float rectangle();
    float triangle();
    float circle();
    float para();
    float calc_rectangle();
    float calc_triangle();
    float calc_circle();
    float calc_para();
    
    ///////////////////////////////////////////////////////////////////////////
    
    // Variables
    //static float stack[1024]; /* 1024 should be MORE than enough */
    
    char r;
    char t;
    char c;
    char p;
    float length;
    float width;
    float height;
    float radius;
    float area;
    char choice;
    
    ////////////////////////////////////////////////////////////////////////////
    
    void main()
    {
    	// Variables
    	char input;
    
    ////////////////////////////////////////////////////////////////////////////
    
    	cout << "This program will allow the user to display the formula for\n";
    	cout << "calculating the area of a figure.\n";
    	cout << "\n";
    	cout << "Do you wish to continue? ";
    	cin >> input;
    
    	while(input == 'y' || 'Y') 			// Begin while loop
    	{
    		clrscr();
    
    		cout << "Please select the figure you wish to work with.\n";
    		cout << "\n";
    		cout << "r = rectangle\n";
    		cout << "t = triangle\n";
    		cout << "c = circle\n";
    		cout << "p = parallelogram\n";
    		cout << "\n";
    		cout << "Enter: ";
    		cin >> choice;
    		cout << "\n";
    
    		if (choice == 'r' || choice == 'R')
    		{
    			clrscr();
    			rectangle();
    		}
    
    		else if (choice == 't' || choice == 'T')
    		{
    			clrscr();
    			triangle();
    		}
    
    		else if (choice == 'c' || choice == 'C')
    		{
    			clrscr();
    			circle();
    		}
    
    		else if (choice == 'p' || choice == 'P')
    		{
    			clrscr();
    			para();
    		}
    
    		cout << "\n";
    		cout << "Do you wish to continue?";
    		cin >> input;
    
    	}// End while loop
    }
    
    ////////////////////////////////////////////////////////////////////////////
    
    float rectangle()
    {
    	cout << "The formula for calculating the area of a rectangle is length * width.\n";
    	cout << "\n";
    	cout << "Example: \n";
    	cout << "If the length of the rectangle is 6 and the width is 4 then:\n";
    	cout << "6 * 4 = 24\n";
    	cout << "The area of the rectangle is 24.\n\n";
    
    	area = calc_rectangle();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    
    float triangle()
    {
    	cout << "The formula for calculating the area of a triangle is 1/2 * length * height.\n";
    	cout << "\n";
    	cout << "Example: \n";
    	cout << "If the length of the triangle is 6 and the height is 4 then:\n";
    	cout << "6 / 2 * 4 = 12\n";
    	cout << "The area of the triangle is 24.\n\n";
    
    	area = calc_triangle();
    }
    
    float circle()
    {
    	cout << "The formula for calculating the area of a circle is PI * radius * radius.\n";
    	cout << "\n";
    	cout << "Example: \n";
    	cout << "If the radius of the circle is 6 then:\n";
    	cout << "PI * 6 2\n";
    	cout << "The area of the circle is 24.\n";
    
    	area = calc_circle();
    }
    
    float para()
    {
    	cout << "The formula for calculating the area of a parallelogram is length * width.\n";
    	cout << "\n";
    	cout << "Example: \n";
    	cout << "If the length of the parallelogram is 6 and the width is 4 then:\n";
    	cout << "6 * 4 = 24\n";
    	cout << "The area of the parallelogram is 24.\n";
    
    	area = calc_para();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // This function will calculate the area of a rectangle
    
    float calc_rectangle()
    {
    	cout << "Enter the length of the rectangle: ";
    	cin >> length;
    	cout << "Enter the width of the rectangle: ";
    	cin >> width;
    
    	area = length * width;
    
    	cout << "\n";
    	cout << "The area of the rectangle is: " << area << "\n";
    
    	return(0);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // This function will calculate the area of a triangle.
    
    float calc_triangle()
    {
    	cout << "Enter the length of the triangle: ";
    	cin >> length;
    	cout << "Enter the height of the triangle: ";
    	cin >> height;
    
    	area = (1/2) * length * height;
    
    	cout << "\n";
    	cout << "The area of the triangle is: " << area << "\n";
    
    	return(0);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // This function will calculate the area of a cicle.
    
    float calc_circle()
    {
    	cout << "Enter the radius of the circle: ";
    	cin >> radius;
    
    	area = PI * radius * radius;
    
    	cout << "\n";
    	cout << "The area of the circle is: " << area << "\n";
    
    	return(0);
    }
    
    float calc_para()
    {
    	cout << "Enter the length of the parallelogram: ";
    	cin >> length;
    	cout << "Enter the width of the parallelogram: ";
    	cin >> width;
    
    	area = length * width;
    
    	cout << "\n";
    	cout << "The area of the parallelogram is: " << area << "\n";
    
    	return(0);
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    Quote Originally Posted by Curtux
    When I run the program I get a floating point stack underflow error.
    I have searched but I can't seem to seem to find the solution.
    Sure we can help, but could you tell us which function you are attempting to calling when you get the error.

    Quote Originally Posted by Curtux
    Any other errors are not my worry now just the stack underflow.
    Well, they are my worry. I'm not sure the code you provided would compile without errors, at the very least there should be many warnings, i.e. functions that should return a value but don't, etc...

    Code:
    /* This program will allow the user to display the formula for calculating
        the area of a figure.
        r = rectangle
        t = triangle
        c = circle
        p = parallelogram
    */
    
    ///////////////////////////////////////////////////////////////////////////
    
    #include <iostream.h> // Should be using <iostream> if available
    #include <conio.h>
    using namespace std; // Addresses issue of namespaces if you change header as suggested
    #define PI 3.141592
    
    // Function prototype
    // None of the functions below actually appear to need to return any value judging
    // by your implementation of them further below
    float rectangle();
    float triangle();
    float circle();
    float para();
    float calc_rectangle();
    float calc_triangle();
    float calc_circle();
    float calc_para();
    
    ///////////////////////////////////////////////////////////////////////////
    
    // Variables
    //static float stack[1024]; /* 1024 should be MORE than enough */
    
    // These really should be local variables, there is no great need for them to be global
    char r;
    char t;
    char c;
    char p;
    float length;
    float width;
    float height;
    float radius;
    float area;
    char choice;
    ////////////////////////////////////////////////////////////////////////////
    
    void main()  // int main always
    {
        // Variables
        char input;
    
    ////////////////////////////////////////////////////////////////////////////
    
        cout << "This program will allow the user to display the formula for\n";
        cout << "calculating the area of a figure.\n";
        cout << "\n";
        cout << "Do you wish to continue? ";
        cin >> input;
    
        // The part in red below should be input == 'y' || input == 'Y'
        while(input == 'y' || 'Y')             // Begin while loop
        {
            clrscr();
    
            cout << "Please select the figure you wish to work with.\n";
            cout << "\n";
            cout << "r = rectangle\n";
            cout << "t = triangle\n";
            cout << "c = circle\n";
            cout << "p = parallelogram\n";
            cout << "\n";
            cout << "Enter: ";
            cin >> choice;
            cout << "\n";
    
            if (choice == 'r' || choice == 'R')
            {
                clrscr();
                rectangle();
            }
    
            else if (choice == 't' || choice == 'T')
            {
                clrscr();
                triangle();
            }
    
            else if (choice == 'c' || choice == 'C')
            {
                clrscr();
                circle();
            }
    
            else if (choice == 'p' || choice == 'P')
            {
                clrscr();
                para();
            }
    
            cout << "\n";
            cout << "Do you wish to continue?";
            cin >> input;
    
        }// End while loop
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // As mentioned above, you have no return statement
    // indicating that the function returns a value although you do 
    // have a return type specified
    float rectangle()
    {
        cout << "The formula for calculating the area of a rectangle is length * width.\n";
        cout << "\n";
        cout << "Example: \n";
        cout << "If the length of the rectangle is 6 and the width is 4 then:\n";
        cout << "6 * 4 = 24\n";
        cout << "The area of the rectangle is 24.\n\n";
    
        area = calc_rectangle();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // As mentioned above, you have no return statement
    // indicating that the function returns a value although you do 
    // have a return type specified
    float triangle()
    {
        cout << "The formula for calculating the area of a triangle is 1/2 * length * height.\n";
        cout << "\n";
        cout << "Example: \n";
        cout << "If the length of the triangle is 6 and the height is 4 then:\n";
        cout << "6 / 2 * 4 = 12\n";
        cout << "The area of the triangle is 24.\n\n";
    
        area = calc_triangle();
    }
    
    // As mentioned above, you have no return statement
    // indicating that the function returns a value although you do 
    // have a return type specified
    float circle()
    {
        cout << "The formula for calculating the area of a circle is PI * radius * radius.\n";
        cout << "\n";
        cout << "Example: \n";
        cout << "If the radius of the circle is 6 then:\n";
        cout << "PI * 6 2\n";
        cout << "The area of the circle is 24.\n";
    
        area = calc_circle();
    }
    
    // As mentioned above, you have no return statement
    // indicating that the function returns a value although you do 
    // have a return type specified
    float para()
    {
        cout << "The formula for calculating the area of a parallelogram is length * width.\n";
        cout << "\n";
        cout << "Example: \n";
        cout << "If the length of the parallelogram is 6 and the width is 4 then:\n";
        cout << "6 * 4 = 24\n";
        cout << "The area of the parallelogram is 24.\n";
    
        area = calc_para();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // This function will calculate the area of a rectangle
    // At least you do have a return statement here although it
    // serves no usefull purpose
    float calc_rectangle()
    {
        cout << "Enter the length of the rectangle: ";
        cin >> length;
        cout << "Enter the width of the rectangle: ";
        cin >> width;
    
        area = length * width;
    
        cout << "\n";
        cout << "The area of the rectangle is: " << area << "\n";
    
        return(0);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // This function will calculate the area of a triangle.
    // At least you do have a return statement here although it
    // serves no usefull purpose
    float calc_triangle()
    {
        cout << "Enter the length of the triangle: ";
        cin >> length;
        cout << "Enter the height of the triangle: ";
        cin >> height;
    
        // This is an integer division operations and the result is 0
        // Any time you call this function the output will ALWAYS be 0
        // Replace the 1/2 with a simple 0.5f
        area = (1/2) * length * height;
    
        cout << "\n";
        cout << "The area of the triangle is: " << area << "\n";
    
        return(0);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // This function will calculate the area of a cicle.
    // At least you do have a return statement here although it
    // serves no usefull purpose
    float calc_circle()
    {
        cout << "Enter the radius of the circle: ";
        cin >> radius;
    
        area = PI * radius * radius;
    
        cout << "\n";
        cout << "The area of the circle is: " << area << "\n";
    
        return(0);
    }
    
    // At least you do have a return statement here although it
    // serves no usefull purpose
    float calc_para()
    {
        cout << "Enter the length of the parallelogram: ";
        cin >> length;
        cout << "Enter the width of the parallelogram: ";
        cin >> width;
    
        area = length * width;
    
        cout << "\n";
        cout << "The area of the parallelogram is: " << area << "\n";
    
        return(0);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > while(input == 'y' || 'Y')
    Try while(input == 'y' || input == 'Y')

    When I compile, I get
    prog.cpp(108) : error C4716: 'rectangle' : must return a value
    prog.cpp(122) : error C4716: 'triangle' : must return a value
    prog.cpp(134) : error C4716: 'circle' : must return a value
    prog.cpp(146) : error C4716: 'para' : must return a value


    > area = (1/2) * length * height;
    1/2 is done in integer math (the result is always 0), so the entire result is also 0

    No floating point underflow here - which compiler did you use (Turbo C ?)

    The void main is wrong as well.

    Also, you really should try using local variables, and passing parameters. A bunch of global variables and void functions is not good style.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  4. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  5. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM

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