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

时间:2010-11-27 22:32:52

标签: 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));
}

1 个答案:

答案 0 :(得分:5)

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

其他小事:

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

除非== b == c,否则此行将拒绝所有三角形。您是否打算限制等边三角形?

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