Program isnt reading values correctly (only in some circumstances)

Hi, Im very new to c++ and I have to make a program for a uni project... I managed to wing my way through most of it, and I thought it was working until I came to writing the report and found that for some reason, when I input the values 1, 14, 7, 290, 350, 5, 0.5, 0.6, 5.2, 0.5, 0.5, 0.5, 1, in that order, it tells me it will fail even though it quite clearly shows the stress to be less than the strength (i.e. the beam should be safe). When I input for example, all 1's, or all 2's, the output is fine. And when the stress/strength values are all under 1, it usually correctly tells me the beam is safe. I dont understand why it just doesnt like certain values.



I cant stress enough that I only understand the basics in C++, so please dont be too harsh on the code, I know its really basic!

Thanks a lot.

Edit, so this thread doesnt get clogged up with code, I've put the new code in two posts further down (it wouldnt fit in one). Oh, and it still wont work.
Last edited on
Please, anyone?
Hi, few things:

1. Lines shouldnt be longer than about 80 characters, as it makes it significantly more difficult to read. In the initial declaration of the float variables, you should try to separate that onto different lines (again, approximately 80 characters per line).

2.
1
2
3
4
5
6
7
8
9
10
11
12
	if (timber > 2)
		{
			cout << "Please start again and enter either a '1' for hardwoods or a '2' for softwoods" << endl;			// Program terminates
		return 0;
		}

	else if (timber == 1)
		{ 
			cout << "You have entered hardwood" << endl;
	}
		else 
			cout << "You have entered softwood" << endl;


You would be better off doing a loop for this. For example:

1
2
3
4
5
6
7
8
    char choice;
    cout << "Enter y for yes, or n for no";
    cin >> choice;
    while(choice != 'y' && choice != 'n')
    {
        cout << "Invalid selection. Please re-enter: ";
        cin >> choice;
    }



Will look at again shortly but will be busy for 20-30 minutes
Ah! Thankyou very much. I never knew what char actually meant until now... Unfortunately though the code still has the same problem :(

Edit: Ah, I cant actually put the edited code back in, its too long... but I have made all the lines shorter (by putting the outputs on a separate lines for example) and added the char function like you said, with my own values obviously.
Last edited on
I've now put all the code into different functions, one for inputs, one for outputs, and one for the message displayed at the end. Still giving me wrong values though.
I'd help you out more, however I'm not familiar with this calculation. What I recommend is...

1. Where-ever possible, divide the program into subroutines (ie. functions). Each function should have a specific purpose which is unique (or close to it) from each other routine.

2. The following code- I'm not sure what you're trying to do with it...

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
		bool isSafe;
		isSafe = true;

		while (isSafe)					
		{
			
			cin.clear();
			cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');	


			
			if ((design_strength < bending_stress) && (designshear >= shearstress))
			{
				cout << "The shear on your beam is fine, but your timber is beyond the bending limit. " << endl;
				cout << "You must increase the width/depth, decrease the load on the beam" << endl;
				cout << "or choose a stronger beam!" << endl;
				isSafe = false;
			}
			else if ((design_strength >= bending_stress) && (designshear < shearstress))
			{
				cout << "Your beam bending is fine, but your shear is not! " << endl;
				cout << " You must increase the depth or width, decrease the length " << endl;
				cout << "or decrease the load on your beam! " << endl;
				isSafe = false;
			}
			else if  ((design_strength < bending_stress) && (designshear < shearstress))
			{
				cout << "The beam bending and shear stress are higher than their maximum values. " << endl;
				cout << "You must increase the width/depth, decrease the load on the beam " << endl;
				cout << "or choose a stronger beam!" << endl;
				isSafe = false;
			}
			else if ((design_strength > bending_stress) && (designshear > shearstress))						// Beam is safe only when these two conditions are met
			{
				cout << "Your beam bending and shear are fine, your beam is safe!" << endl;
				isSafe = true;
			}
			return 0;
		} 


I dont know why you include a "while" loop. Additionally, you should test the conditions individually. If you have a lot of if-else-if statements chained, then it is probably a hint that this isn't a good design. I'm not some expert programmer, however I would likely write it like this...


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
    bool isStrengthSufficient;
    bool isDesignShearSufficient;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    if(design_strength < bending_stress)    // Is the design strength insufficient?
        isStrengthSufficient = false;
    else
        isStrengthSufficient = true;

    if(designshear < shearstress)
        isDesignShearSufficient = false;
    else
        isDesignShearSufficient = true;

    if(isStrengthSufficient == true && isDesignShearSufficient == true) // Is the beam strong enough?
    {
        cout << "Your beam is safe!" << endl;
    } else { // This block of code executes if the beam is not strong enough, implying one or both of the above bools evaluates to false
        if(isStrengthSufficient == false)
            cout << "Your design strength is insufficient" << endl;
        if(isDesignShearSufficient == false)
            cout << "Your design shear is insufficient" << endl;
    }
    return 0;




Edit: Also, the code below...

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
		switch (service_class)
	{
		case 1:
			{ 
				permanent_action_factor = 0.6;
				break;
			}

		case 2:
			{
				permanent_action_factor = 0.6;
				break;
			}
		
		case 3:
			{
				permanent_action_factor = 0.5;
				break;
			}
		default:
			{
				cout << "Error, bad input! \n" ;
					break;
					return 0;
			}
	}



Since there are only two possible outcomes (ignoring invalid input), you would likely be better off using an if/else statement.
Last edited on
Thanks for the reply.

I understand what you're saying, and I tried changing the end to what you said (the reason I had it as a while was only because I needed to use a variety of different techniques) but its still giving me the same problem... it wont take in those specific values and tell me the beam is safe, even though I can clearly see the stress is lower than the strength.

I really wish I knew why it was doing that. Heres the code I currently have, although I have to post it in two parts:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cmath> 
#include <algorithm>

using namespace std;

void display_message (int n);
void inputs (int n);
void outputs (int n);

int service_class;
float timber, bending, moe_parallel, density, mean_density, length, depth, width;
float variable_load, kmod, joist, choice, self_weight, perm_load;
float tension_parallel, tension_perp, comp_parallel, comp_perp, shear, five_parallel;
float mass, Design_action, max_moment, secondmoment, design_stress, bending_stress;
float moe_perp, mean_shear, design_strength, Vd, shearstress, designshear;





int main ()
{

		cout << "This program is designed to calculate whether or not your solid timber beam " << endl;
		cout << "will be able to withstand a given load (in shear and tension/compression)." << endl;
		cout << "Enjoy!" << endl;

			inputs (1);
			outputs (1);
			display_message (1);
			system ("PAUSE");
			return 0;

}

void inputs (int n)
{

	cout << "Firstly, is your timber a hardwood or a softwood?" << endl;				// For later calculations, factors vary in softwoods and hardwoods
		char choice;
		cout << "Enter 1 for hardwood, or 1 for softwood" << endl;
		cin >> choice;
    while(choice != '1' && choice != '2')
    {
        cout << "Invalid selection. Please re-enter: ";
        cin >> choice;
    }

	cout << "Now please find the beam stiffness and strength properties, commonly found" << endl;
	cout << " in the 'Strength Class' tables for softwood and hardwood." << endl;
	cout << "Enter the characteristic value for bending strength (Classed under " << endl;
	cout << " 'Strength Properties') [in N/mm^2]: "<< endl;							// Fewest number of inputs without using an array
	cin >> bending;

	cout << "Now please enter the mean characteristic value of modulus of " << endl;
	cout <<	"elasticity parallel to the grain (found under 'Stiffness Properties')" << endl;
	cin >> moe_parallel;

	cout << "The following information is also required:" <<endl;

	cout << "Enter the density [in kg/m^3]" << endl;
	cin >> density;

	cout << "Enter the mean density [in kg/m^3]" << endl;
	cin >> mean_density;

	cout << "Enter the beam length [in m]" <<endl;
	cin >> length;

	cout << "Enter the beam depth [in m]" << endl;
	cin >> depth;

	cout << "Enter the beam width [in m]" << endl;
	cin >> width;

	cout << "Enter the distance between joists. If joists do not exist or " << endl;
	cout << "are not known, input the beam length [in m]" << endl;				
	cin >> joist;							// Possibly problematic if joists are not used, but info is needed for shear values 

	cout << "Enter the self weight of beam [in kN/m^2]" << endl;
	cin >> self_weight;

	cout << "Enter the permanent loading [in kN/m^2]" << endl;
	cin >> perm_load;

	cout << "Enter the variable loading [in kN/m^2]" << endl;
	cin >> variable_load;

} 
Last edited on
Part 2:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
void outputs (int n)

{

	tension_parallel = (0.6*bending);
		cout << "The tensile strength parallel to grain is " << endl;
			cout << tension_parallel << "N/mm^2" << endl;

	comp_parallel = (5*(pow((double)bending, 0.45)));
		cout << "The compressive strength parallel to grain is " << endl;
			cout << comp_parallel << "N/mm^2" << endl;

	tension_perp = min((0.0015*density), 0.6);
		cout << "The tensile strength perpendicular to grain is " << endl;
			cout << tension_perp << "N/mm^2" << endl;

		{
			if (timber == 1)					// hardwood/softwood value takes into account different factors for the following calculations
			{
				comp_perp = (0.0015*density);
				five_parallel = (0.84*moe_parallel);
				moe_perp = (moe_parallel/15);
				shear = (min(0.2*(pow((double)bending, 0.8)), 3.8));
					
			}

			else 
				{
					comp_perp = (0.007*density);
					five_parallel = (0.67*moe_parallel);
					moe_perp = (moe_parallel/30);
					shear = (0.2*(pow((double)bending, 0.8)));
				}
			cout << "The compressive strength perpendicular to the grain is " << endl;
				cout << comp_perp << "N/mm^2" << endl;
			cout << "The 5-percentile modulus of elasticity parallel to grain is " << endl;
				cout << five_parallel << "kN/mm^2" << endl;
			cout << "The mean modulus of elasticity perpendicular to grain is " << endl;
				cout << moe_perp << "kN/mm^2" << endl;
			cout << "The shear strength is " << endl;
				cout << shear << "N/mm^2" << endl;
		}
			


	mean_shear = (moe_parallel/16);
		cout << "The mean shear modulus is " << endl;
			cout << mean_shear << endl;

	Design_action = (1.35*(self_weight + perm_load) + (1.5*variable_load));
		cout << "The Design Action (total loading) is: " << endl;
			cout << Design_action << "kN/m^2" << endl;

	max_moment = ((Design_action*length)/8);
		cout << "The maximum Moment is " << endl;
			cout << max_moment << "kN/m^2" << endl;


	secondmoment = ((width * (pow(depth, 3)))/12);
		cout << "The Second Moment of Area is " << endl;
			cout << secondmoment << "m^4" << endl;
	

	
	bending_stress = ((max_moment*depth/2)/secondmoment);
		cout << "The total Bending Stress is " << endl;
			cout << bending_stress << "kN/m^2" << endl;




	cout << "Please enter the Service Class number [1, 2 or 3]" << endl;			// This is used to find k(mod) for solid timber. Values taken from table 3.1 (BS EN 1995-1-1:2004)
	cin >> service_class;

		switch (service_class)
	{
		case 1:
			{ 
				kmod = 0.6;
				break;
			}

		case 2:
			{
				kmod = 0.6;
				break;
			}

		
		case 3:
			{
				kmod = 0.5;
				break;
			}
		default:
			{
				cout << "Error, bad input! \n" ;
					cout << "Please enter the value of k(mod), i.e. the permanent action factor" << endl;
					cin >> kmod;
					
			}
	}

		if 	(depth > 0.15)
			{
				(design_strength = ((kmod*1.1*bending*1.3)/1.3));			// [k(mod) * k(sys) * fk * k(h)] / gamma(m). K(h) is given as 1.3 if depth is above 150mm. 
				
			}

			else
			{
				(design_strength = ((kmod*1.1*bending*(min(pow((double)(0.15/depth), 0.2), 1.3)/1.3))));			// Here k(h) is the minimum of two values, when d <= 150mm
			}

		cout << "The Design Strength is: " << endl;
			cout << design_strength << "kN/m^2" << endl;

		design_stress = ((double)Design_action/(width*depth));
		cout << "The Design Stress is: " << endl;
			cout << design_stress << "kN/m^2" << endl;			// Compare this with design strength to know whether the beam will fail


		Vd = (Design_action*(pow((double)10, 3))/(joist*length));			// Problematic if joist length is not applicable

		shearstress = (((3*Vd)/(2*(depth*width)))/1000);
			cout<<"The Shear Stress is " << endl;
				cout << shearstress << "kN/m^2" << endl;

		designshear = (((shear*(pow((double)10, 6))*kmod*1.1)/1.3)/1000);
			cout<<"The Design Shear Strength is " << endl;
			cout << designshear << "kN/m^2" << endl;				// Compare with shear stress to know whether beam will fail in shear


}

void display_message (int n)
{
	bool isStrengthSufficient;
    bool isDesignShearSufficient;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    if(design_strength >= bending_stress)    // Is the design strength insufficient?
        isStrengthSufficient = true;
    else
        isStrengthSufficient = false;

    if(designshear >= shearstress)
        isDesignShearSufficient = true;
    else
        isDesignShearSufficient = false;

    if(isStrengthSufficient == true && isDesignShearSufficient == true) // Is the beam strong enough?
    {
        cout << "Congratulations, your beam is safe!" << endl;
    } 
	else
	{			// This block of code executes if the beam is not strong enough, implying one or both of the above bools evaluates to false
        if(isStrengthSufficient == false)
            cout << "Your design shear is fine, but your design strength is insufficient." << endl;
				cout << "You must increase the width/depth, or decrease the load on the beam" << endl;
        if(isDesignShearSufficient == false)
            cout << "Your design strength is fine, but your design shear is insufficient." << endl;
				cout << "You must increase the width/depth, decrease the load on the beam." << endl;
    }
   
			
			
}	 
Topic archived. No new replies allowed.