Calculations in a for loop not working properly
Sep 26, 2010 at 8:08pm UTC
Hi everyone,
I'm a total beginner to c++ and I am having a bit of trouble with calculations within a for statement the goal of the program is to calculate the stresses in a truss system based on the values inputted by the user. I used multiple for loops so that the user can input all the required data for the table and a for loop to calculate the values and output them in the format I want. Problem is the first iteration of the calculations doesn't work properly while the second and the third do. Even though its probably something simple I can't seem to figure it out, if anyone could point me in the right direction it would be much appreciated.
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
//
//
//
//
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <stdio.h>
int main()
{
float const PI=3.141519;
int run=1, x=0,row=1;
float force_p[2], theta_p[2], theta_a[2], theta_b[2],theta_ar[2];
float theta_br[2], theta_pr[2],force_a[2], force_b[2], force_px[2];
float force_py[2], D[2];
while (run==1)
{
//Setting IOS parameters
cout<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint)
<<setprecision(2);
//Variable input
for (x;x<=2;x++)
{
cout<<"\n Row = " <<row<<"\n" ;
cout<<"\nInput Force (P)\n" ;
cin>>force_p[x];
cout<<"\nInput angle of P with respect to the positive x-axis\n" ;
cin>>theta_p[x];
cout<<"\nInput angle of truss A to the positive x-axis\n" ;
cin>>theta_a[x];
cout<<"\nInput angle of truss B to the positive x-axis\n" ;
cin>>theta_b[x];
row++;
//Mass conversion of theta degrees into radians
theta_ar[x]=theta_a[x]*PI/180;
theta_br[x]=theta_b[x]*PI/180;
theta_pr[x]=theta_p[x]*PI/180;
//Number Crunching
D[x]=sin(theta_b[x]-theta_a[x]);
force_px[x]=force_p[x]*cos(theta_pr[x]);
force_py[x]=force_p[x]*sin(theta_pr[x]);
force_a[x]=(force_py[x]*cos(theta_br[x])-force_px[x]*sin(theta_br[x]))/D[x];
force_b[x]=(force_px[x]*sin(theta_ar[x])-force_py[x]*cos(theta_ar[x]))/D[x];
}
x=0;
cout<<" AXIAL LOADS IN A TWO BAR PINNED TRUSS\n\n\n"
<<" TRUSS NO" <<setw(9)<<"TRUSS" <<setw(10)<<"LOAD" <<setw(10)<<"LOAD\n"
<<setw(18)<<"LOAD" <<setw(12)<<"BAR-A" <<setw(10)<<"BAR-B\n"
<<setw(19)<<"(LBS)" <<setw(11)<<"(LBS)" <<setw(10)<<"(LBS)\n" ;
row=1;
//COUTing
for (x;x<=2;x++){
cout<<"\n" <<setw(6)<<row<<setw(14)<<force_p[x]<<setw(11)<<force_a[x]<<setw(10)<<force_b[x]<<"\n" ;
row++;
}
x=0;
row=0;
cout<<"\n\n\nagain? 1 or 0\n" ;
cin>>run;
}
return 0;
}
Sep 27, 2010 at 7:13am UTC
Suggestions:
1. fix your formatting - I can't tell where your blocks start and end
2. learn to use a debugger
Last edited on Sep 27, 2010 at 7:15am UTC
Topic archived. No new replies allowed.