函数在通过while循环时不更新变量

时间:2015-10-26 13:24:24

标签: c++ loops variables while-loop

#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;
//resistivity ohms*meters
/*
 * 
 */
double diameter (double wiregauge)
{
  double k;
  double diam;

    k=((36-wiregauge)/39);
    diam=(.127)*(pow(92,k));
    diam=(diam/100);

  return diam;
}
double copperWireResistance(double length, double wiregauge)
{
    const long double PI=(atan(1)*4);
    int b; //equals k in the first function
    double diam1; //diameter of the wire
    double cross_area_copper; //denominator of the equation
    double numerator1; //numerator of the equation
    double p; //resistivity of copper
    double coppWireResistance;

    b=((36-wiregauge)/39);
    diam1=(.127*pow(92,b));
    diam1=(diam1/100);
    cross_area_copper=PI*(pow(diam1,2));
    p=(1.678*(pow(10,-8)));
    numerator1=((4*p)*length);

    coppWireResistance=numerator1/cross_area_copper;

        return coppWireResistance;

}

double aluminumWireResistance(double length, double wiregauge)
{
    const long double PI=(atan(1)*4);
    int a;
    double diam2;
    double numerator2;
    double cross_area_alum;
    double p1;
    double alumWireResistance;

    a=((36-wiregauge)/39);
    diam2=(.127*pow(92,a));
    diam2=(diam2/100);
    cross_area_alum=(PI*pow(diam2,2));
    p1=(2.82*pow(10,-8));
    numerator2=((4*p1)*length);

    alumWireResistance=numerator2/cross_area_alum;

        return alumWireResistance;
}


int main(int argc, char** argv) {

    double Max_N; //maximum wire gauge value
    double L; //Length of the wire
    double string_gauge=0;

    cout<<"Maximum wire gauge value: ";
    cin>>Max_N;
    cout<<"Length of Wire: ";
    cin>>L;


    while(string_gauge<Max_N)
    {
    cout<<"String Gauge: "<<string_gauge<<endl;
    cout<<"Diameter of wire: "<<diameter(string_gauge)<<endl;
    cout<<"Copper Wire Resistance: "<<copperWireResistance(L,string_gauge)<<endl;
    cout<<"Aluminum Wire Resistance: "<<aluminumWireResistance(L,string_gauge)<<endl;


    string_gauge++;
    cout<<endl;
    }



    return 0;
}

我试图让铜线耐受性和铝线耐受性在它通过循环时更新,但是在第一个数字之后它没有更新到新的弦规。我需要添加任何理由或任何东西吗?

3 个答案:

答案 0 :(得分:1)

这两个函数中的wiregauge参数用于以下计算:

a=((36-wiregauge)/39); // in aluminumWireResistance
b=((36-wiregauge)/39); // in copperWireResistance

由于abintwiregauge是唯一一个从呼叫转为另一个参数的参数。 ab将始终等于0。并且函数的返回值将始终相同。

要解决此问题并提高精确度,您可以将a的类型更改为double

答案 1 :(得分:0)

int aa=((36-wiregauge)/39);中杀死你的师,其余的从那里开始不变,直到你消极为止。

答案 2 :(得分:0)

ab函数中的copperWireResistancealuminumWireResistance变量存在一些问题。你把它们声明为int,但是在你的等式中你获得了一个值,即少于一个值。

与结果一样,您总是从函数返回相同的值。

因此,将类型更改为浮动加倍

PS。如果您将数字与&lt; 运算符进行比较,请将Max_N类型更改为 int long