Am working on a small problem and have spent quite a few hours trying to figure out what I did wrong. Using Dev++ compiler which at times has some cryptic error messages.
I tried to make the Volume calculation a function and got it to work but I have 2 small nits. Will work on error checking after I resolve this.
-
With the function added, for some reason with dev++ now, the program does not pause (press any key to continue).
-
Volume is coming up with blank instead of a number.
Thanks
PC
// 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";
calcvolume(area,height);
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;
}
Your updated code looks correct, but you aren’t storing the calcvolume return value. The volume variable you declare in calcvolume is different than the one you declare in main. Each of these variables can only be referenced from within the function it is declared in.
In order to save the volume,
calcvolume(area,height);should be
volume = calcvolume(area,height);This will store the value being returned from calcvolume in the
volumevariable in your main function.