Need help on C++ program function call and pausing

pacumming
pacumming used Ask the Experts™
on
Working on a newbie program and I have it where it does run and call a function. However if you enter values of 12 for area and 12 for height, the volume should be 48. This works properly without a function but with the function I get nothing for the value of volume. We have to do this as a "double".

Second issue is that without a function the program would pause in the DOS box with "press any key to continue". With the function it does not pause any more.
Am using Dev C++ 4.9 by Bloodshed software
Thank you
PC
volfunc.cpp
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Commented:
You only forgot to assign the return value, see the following commented code (pausing does work, BTW):
// The purpose of this program is to determine the Volume of a
// square-based pyramid after the user inputs the Area and
// the Height.
#include <iostream>
#include <iomanip>
using namespace std;
double calcvolume(double a, double h) 
{ 
    double volume;
    volume = ( a * h ) / 3; 
    
    return (volume);
    
} 

int main()
{                                 
   double area, height, volume;                    // declare variables
   cout << "Please enter the Area of the Square-based pyramid:";       // requests users input
   cin >> area;                                                        // assigns user input to area
                                                                       
   cout << "Please enter the Height of the Square-based pyramid:";     // requests user input
   cin >> height;    
                                                   // assigns user input to height
   cout << "Area= " << area << "\n";                                   // Prints user input for area
   cout << "Height= " << height << "\n"; 
 
   volume = calcvolume(area,height);  // <---------- assign return value here
     cout << "Volume test =" << volume; 
        
   cout << "Volume= " << fixed << showpoint << setprecision(2) << volume << "\n";    // Prints resolution to 

the formula stored in volume
    
    system("pause"); // forces DOS window to pause to allow user to utilize program
    return 0;
}

Open in new window

re: system("pause");
It worked in Visual Studio 2008 Express; it did not work using cygwin.

re: pause
Should work on DOS:
C:\Program Files\Microsoft Visual Studio 9.0\VC>help pause
Suspends processing of a batch program and displays the message
    Press any key to continue . . .

So, now that the function is fixed, your pause now works again, right?
Even with the uninitialized variable, volume, the pause works fine on Visual Studio 2008 Express.

Not sure why your pause did not work with the function; but I'm guessing that the volume variable on the stack had garbage in it causing a conversion problem from double to ascii. Some bit representations are, I believe, not a valid number, and possibly that cause a ripple effect.
To make your pausing more portable, you could replace system("pause") with:
systemPause();

Note that whitespace keys (e.g., enter, tab, space) will not cause systemPause() to return.
void systemPause() {
	char anyLetter;
	cout << "Press any letter to continue . . . ";
	cin >> anyLetter;
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial