-2

I am trying to produce this code but have and error in the double functions starting at double b[n];. The error I am getting is saying that "the expression must have a constant value, the variable 'n' can not be used as a constant. Any help you can give would be much appreciated.

//Get inputs from user

double V = 0;    // shear load applied
int n;
double H_total = 0;
double A_total = 0;
double a = 0;
double I = 0;
double t = 0;
double e = 0;
double y_bar = 0;

cout << "Input the shear load applied in [N]: " << endl;
cin >> V;

cout << "Input number of sections: " << endl;
cin >> n;

double b[n];
double h[n];
double A[n];
double y[n];
double Q[n];
double Tau[n];
for (int i = 1; i <= n; i++) { // Calculates variables to find shear stress

    cout << "Width of section " << i << " in [mm]: " << endl;
    cin >> b[i];

    cout << "Height of section " << i << " in [mm]: " << endl;
    cin >> h[i];

    H_total += h[i];
    A[i] = b[i] * h[i];
    A_total += A[i];
    y[i] = H_total - 0.5 * h[i];
    a += A[i] * y[i];
    y_bar = a / A_total;

}

cout << "Applied shear force, V = " << V / 1000 << " kN" << endl;
cout << "Y coordinate of the centroid for given cross section, Y_Bar = " << y_bar << " mm" << endl;

for (int i = 1; i <= n; i++) { // Finds moment of inertia

    double d = (y[i] - y_bar);

    I += (b[i] * pow(h[i], 3.0) / 12.0) + (A[i] * pow(d, 2.0));

}

cout << "Moment of Inertia, I = " << I << " mm^4" << endl;

for (int i = 1; i <= n; i++) { // Calculates first moment of inertia

    Q[i] = A[i] * (y[i] - y_bar);

}

for (int i = 1; i <= n - 1; i++) {

    if (b[i] <= b[i + 1]) {

        t = b[i];

    }
    else {

        t = b[i + 1];

    }

    Tau[i] = (abs(V * Q[i]) / (I * t));

}

for (int i = 1; i <= n - 1; i++) {

    if (i <= 2) {

        e += Tau[i];

    }
    else {

        e -= Tau[i];

    }
    cout << "Shear stress between sections " << i << " and " << i + 1 << " = " << e << " MPa" << 
endl;

}
}
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • 4
    Possible duplicate of [c++ array - expression must have a constant value](https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value) – Klaycon Nov 07 '19 at 01:34
  • 3
    Make it `std::vector b(n);` (and similar for other arrays). – Igor Tandetnik Nov 07 '19 at 01:38
  • 1
    Welcome to Stack Overflow. Your question is too vague, and your code is too long. When you write code, don't try to write the whole program at once. Start with something small and simple that works perfectly, then add complexity a little at a time, testing at every step, and *never add to code that doesn't work.* – Beta Nov 07 '19 at 01:48
  • Your title is not descriptive of the problem. A future programmer with the same problem would have no idea that this describes the problem that other programmer is having. I fixed the title. – Raymond Chen Nov 07 '19 at 02:43

1 Answers1

1

First of all double b[n]; is not a function, it is an array. This error is common with 2-D arrays. However, you aren't using any 2-D arrays here. Also, your code has no error unless you provide specific inputs which cause this error. You can see the output for some random inputs:

Applied shear force, V = 0.004 kN
Y coordinate of the centroid for given cross section, Y_Bar = 3.29661 mm
Moment of Inertia, I = 322.476 mm^4
Shear stress between sections 1 and 2 = 0.147082 MPa
Shear stress between sections 2 and 3 = 0.231598 MPa
a4arshad
  • 606
  • 1
  • 7
  • 18
  • How can i fix this issue then? – Matt Backert Nov 07 '19 at 02:00
  • As I mentioned in my answer your code does not generate any error. Try these header files `#include and `. – a4arshad Nov 07 '19 at 02:22
  • I have both of those header files in there and it still does not work. I understand that manually inputting values for the variables works but I need to be able to replicate this based on user input. Is there anyway that I can make that work with this setup? – Matt Backert Nov 07 '19 at 02:38