c++ - 在 C++ 中使用引用变量的 void 返回类型

标签 c++ variables reference

我已经在这个 C++ 源文件上工作了几个星期,但不明白我哪里出错了..

//This program will ask the user for the measurement of all 3 sides of a triangle
//and first determine if the numbers will equal a triangle, then give the area and
//perimeter of the triangle.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void calc(double , double , double ,double &, double &);
void s(double , double , double , double &);

int main()
{       
    double a;
    double b;
    double c;
    double per;
    double sr;
    double areat;

    cout<<"Enter the three side of your triangle: ";
    cin>> a >> b >> c;
    per = a+b+c;

    if (a > b || a < b || b > c || b < c)
    {
        cout<< "Sorry, this is not a triangle.\n";
    }
    else
    {
        cout<<"For a Triangle with the sides of "<<a<<", "<<b<<",and " <<c <<endl; 
        cout<< setprecision(3)<<fixed<<showpoint;
        cout<<"The Perimeter is "<<per<<endl;
        calc(a,b,c,sr,areat);
        cout<< "The Area is "<<areat<<endl;
    } 
    system ("pause");
    return 0; 
}    

void s(double a, double b, double c, double &sr)
{
    sr = (a+b+c)/2;
}

void calc(double a, double b, double c, double &sr, double &areat)
{
    areat = sqrt(sr*(sr-a)*(sr-b)*(sr-c));
}

最佳答案

您没有调用函数 s(),因此变量 sr 没有被初始化。

其他小事:

if (a > b || a < b || b > c || b < c)
    cout << "Sorry, this is not a triangle.\n";

这条线将拒绝所有三角形,除非 a == b == c。您是否打算限制为等边三角形?

在函数calc()中,sr不是输出,不需要通过引用传递。

关于c++ - 在 C++ 中使用引用变量的 void 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4294279/

相关文章:

C++ 分配 vector 指针

python - 记录数据以及如何执行多个响应以及如何在 python 中使用变量

php - 如何使用 foreach 循环动态执行 mysql 查询?

pointers - 如何在 Clojure 中创建引用/指针?

reference - Visual Studio 11 提示 web.config 中的一个命名空间导入

Java - 如果可能的话,传递引用或使用硬编码对象是否更好

c++ - 从 .exe 获取编译日期和时间

C++ 读取内存地址/指针和偏移量

c++ - std::thread::join 无限期地阻塞在 main 之外

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP