Program returning "inf" instead of calculated value

I am new to C++ and in order to practice, I tried writing a program on an online compiler to calculate the Maximum Stress in cantilever beams when placed under load, but instead of getting back a numerical answer for the "MaxStress" variable, I keep getting "inf". I have checked to see if there is any division by 0 or uninitialized variables being used in equations but I am unable to find any errors. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  

// Example program
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//Max Stress of Fixed Beam under unsymmetric eccentric loading

int main()
{
    float Force, FxDistance, FyDistance, BeamWidth, BeamHeight, BeamDepth, MomentX, MomentY, NormalStress, AreaOfAction, AllowableStress, x, y;
    cout << "Please input Allowable Stress in MPa \n";
    cin >> AllowableStress;
    cout << "Please input Force in Newtons \n";
    cin >> Force;
    cout << "Please input the distance from the X-Axis \n";
    cin >> FxDistance;
    cout << "Please input the distance from the Y-Axis \n";
    cin >> FyDistance;
    cout << "Please input the Beam Width in meters \n";
    cin >> BeamWidth;
    cout << "Please input the Beam Depth in meters \n";
    cin >> BeamDepth;
    MomentX = Force*FxDistance;
    MomentY = Force*FyDistance;
    AreaOfAction = BeamWidth*BeamDepth;
    NormalStress = Force/AreaOfAction;
    
    ////////////////////////////////////////////////////////////////////////////////
    
    float BendingStressX, BendingStressY, cx, cy, Ix, Iy, MaxStress;
    Ix = (1/12)*(BeamWidth)*(pow(BeamDepth,3));
    Iy = (1/12)*(BeamDepth)*(pow(BeamWidth,3));
    cx = BeamWidth/2;
    cy = BeamDepth/2;
    BendingStressX = ((MomentX)*(cy))/(Ix);
    BendingStressY = ((MomentY)*(cx))/(Iy);
    
    MaxStress = (BendingStressX + BendingStressY + NormalStress);
    ////////////////////////////////////////////////////////////////////////////////
    
    cout << "Your Maximum Stress is approximately: " << MaxStress << "MPa" << endl;
    
x = MaxStress;
y  = AllowableStress;

    if (x>y)
    { cout << "WARNING: Max Stress exceeds Allowable Stress." << endl;
    }
    else 
    {
    cout << "Max Stress is within safe parameters." << endl;
    }
  cout << "End of Program.";
  return 0;
}



Any and all help is greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
    cout << "Please input Allowable Stress in MPa \n";
    cin >> AllowableStress;
    cout << "Please input Force in Newtons \n";
    cin >> Force;
    cout << "Please input the distance from the X-Axis \n";
    cin >> FxDistance;
    cout << "Please input the distance from the Y-Axis \n";
    cin >> FyDistance;
    cout << "Please input the Beam Width in meters \n";
    cin >> BeamWidth;
    cout << "Please input the Beam Depth in meters \n";
    cin >> BeamDepth;

Give us some numbers to type in.

1
2
    Ix = (1/12)*(BeamWidth)*(pow(BeamDepth,3));
    Iy = (1/12)*(BeamDepth)*(pow(BeamWidth,3));

Note that 1/12 will be done in integer arithmetic, which will serve to make both Ix and Iy zero.

Try say this, to force a floating point 12th.
1
2
    Ix = (1.0/12)*(BeamWidth)*(pow(BeamDepth,3));
    Iy = (1.0/12)*(BeamDepth)*(pow(BeamWidth,3));
Last edited on
You have expression:
(1/12) * BeamWidth * pow(BeamDepth,3)
Q: How much is integer 1 divided by integer 12?
A: The result is an integer value. The 0.

Q: How much is integer 11 divided by integer 12?
A: The result is an integer value. The 0.

Integer division produces an integer and discards the remainder.

If either operand is float, then division uses float math.

Rearrange your terms and remove the redundant 1:
BeamWidth * pow(BeamDepth,3) / 12
You have some significant unit problems. Don't mix newtons, metres and ... MPa (MEGApascals) or you will get factor-of-a-million errors.

You don't need new variables x and y. Just compare MaxStress and AllowableStress directly.

You could simplify your second-moment-of-area calculations and avoid integer division errors by, e.g.,
1
2
Ix = AreaOfAction * BeamDepth * BeamDepth / 12;
Iy = AreaOfAction * BeamWidth * BeamWidth / 12;

The fact that formulae are written with factors as 1/12 or ab3 doesn't necessarily mean that you should code them like that.

I think it would be good practice to output the calculated values (certainly the MaxStress, probably the second moments of area and bending stresses as well). Otherwise you might end up coming to the correct conclusion from incorrect calculations and never know that anything is wrong.
Last edited on
Thank you so much everyone for the advice! My code is now working correctly as it should thanks to the suggestions. I am trying to teach myself at home rather than in a classroom setting so much of this comes as unexpected nuances to me, but I will be sure to write everything down so I remember it!
Topic archived. No new replies allowed.