gpt4 book ai didi

c++ - 关于函数和错误检查的 C++ 新手问题

转载 作者:行者123 更新时间:2023-11-28 08:30:56 26 4
gpt4 key购买 nike

我正在处理一个小问题,并花了好几个小时试图找出我做错了什么。使用有时会出现一些神秘错误消息的 Dev++ 编译器。

我试图使体积计算成为一个函数并让它工作,但我有 2 个小尼特。解决此问题后将进行错误检查。

  1. 添加该功能后,出于某种原因,现在使用 dev++,程序不会暂停(按任意键继续)。

  2. 音量显示为空白而不是数字。

谢谢电脑

// 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;
}

最佳答案

您更新后的代码看起来是正确的,但您没有存储 calcvolume 返回值。您在 calcvolume 中声明的 volume 变量与您在 main 中声明的不同。这些变量中的每一个都只能在声明它的函数中引用。

为了节省音量,

calcvolume(面积,高度);

应该是

volume = calcvolume(面积,高度);

这将从 calcvolume 返回的值存储在主函数的 volume 变量中。

关于c++ - 关于函数和错误检查的 C++ 新手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2149529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com