简体   繁体   English

使用C ++中的引用变量来无效返回类型

[英]void return types using reference variables in c++

I have been working on this C++ source file for weeks and do not understand where I am going wrong at.. 我已经在这个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));
}

You aren't calling function s() , therefore variable sr is not getting initialized. 您没有调用s()函数,因此未初始化变量sr

Other minor things: 其他小事:

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

This line will reject all triangles unless a == b == c. 这条线将拒绝所有三角形,除非a == b == c。 Did you intend to limit to equilateral triangles? 您是否打算限制为等边三角形?

In function calc() , sr is not an output, it does not need to be passed by reference. calc()函数中, sr不是输出,不需要通过引用传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM