I need help with understanding how to fix my for loop and the if tests. Originally, in the for loop, for the end value i had k<=3..but this is wrong because i need to loop to keep going until it passes the test. So i was told to try setting k=0 to make it false and create an if test within the for loop to make it true, so that when it does pass the test, it stops. how does that work?
We were given an example as to what the output should look like (if that helps with understanding what i am asking):
Please enter the expected load in pounds> 9000
Please enter the length of the column in inches> 120
….Testing a beam with Area of 2.0 by 2.0 inches – Failed the tests
….Testing a beam with Area of 4.0 by 4.0 inches – Failed the tests
….Testing a beam with Area of 6.0 by 6.0 inches – Passed the tests
But here is my original code
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
//global constants
#define E 1700000
#define COMP_STRENGTH 445
//list function prototypes
double buckload(double area, double length, double width);
double compstress(double area);
double slender (double length, double width);
int main(void)
{
double buck_load, area, length,ratio,max,width;
double stress,load;
ofstream outfile; /*declare var to write to a file*/
outfile.open("hw4c.txt"); /*open output file hw4c*/
outfile<<"Ugonna Ibe\n";
outfile<<"hw4c\n\n";
cout<<"Please enter expected load in pounds> ";
cin>>load;
cout<<"Please enter the length of the column in inches> ";
cin>>length;
for(int k=1;k=0;k++){
width=k*2;
area=width*width;
ratio=slender(length,width);
buck_load=buckload(area,length,width);
stress=compstress(area);
if (ratio<=50 && load<=buck_load && load<=stress){
cout<<"\nTesting a beam with Area of "<<width<<" by "<<width<<" inches-Passed the tests\n";
cout<<"\n\nFor a load of "<<load<<" pounds and a length of "<<length<<" inches,\nrecommended square beam has sides of "<<width<<" inches.\n";
outfile<<"\nFor a load of "<<load<<" pounds and a length of "<<length<<" inches, recommended square beam has sides of "<<width<<".";
}
else{
cout<<"Testing a beam with Area of "<<width<<" by "<<width<<" inches-Failed the tests\n";
outfile<<"\nDo not use "<<width<<" for a load of "<<load<<".";
}
}
system("pause");
return 0;
}
double buckload(double area, double length, double width)
{
double x;
x=(.3*E*area)/((length / width) * (length / width));
return(x);
}
double compstress(double area)
{
double y;
y=area*COMP_STRENGTH;
return (y);
}
double slender (double length, double width)
{
double z;
z=length/width;
return (z);
}