开发者

Newbie C++ question about functions and error checking

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.

  1. With the function added, for some reason with dev++ now, the program does not pause (press any key to continue).

  2. 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;                                      开发者_如何学JAVA                  // 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 volume variable in your main function.


You have to assign the result of calcvolume(area,height) to main's volume as follows:

volume = calcvolume(area,height);

Now you can safely use main's volume variable.

I'm guessing that your program was not even reaching the system("pause") line, and was crashing the line above. It could be because volume was never set to anything and was holding garbage data. This garbage data made cout << ... fail.

Before you fix the calcvolume(area,height) line, try fixing your variable declarations so that your variables are initialized to zero:

double area=0.0, height=0.0, volume=0.0;  // declare variables

Now run it again and see if it outputs Volume=0.00 and pauses.

It's always good to initialize your variables to zero or something meaningful. Otherwise, they will be initialized to random data (whatever was already in those memory bytes) and will make troubleshooting more difficult.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜