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 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)
{

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 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);
stress=compstress(area);
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";
}
}
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);
}``````

Could you use a while loop? Here is an example of a while loop that runs until a condition is met:

``````bool x=false;
int num = 0;
while(!x){//while x is false
//test
if(num ==5){
//if pass:
cout<<"true";
x = true;
}
else{ //if fail:
cout<<num<<endl;
num++;
}
//should exit when num is 5
}``````

What is the condition that causes TRUE? What == what? That goes in the 2nd part (condition) of the for() loop.

The condition does not have to be based on k, but if you are running from k=0,1,2... until val == 10 (something calculated in the loop), you can certainly use `for (k=0; val != 10; k++)` Example

``````for (k=0; val < 10; k++)
{
val = k/2;
}``````

Also in line 30 you have

``for (int k = 1; k = 0; k++)``

the problem is that you are not testing for anything, you are reassigning k to 0 because you are using the single equals

Your main issue, as hag++ mentioned, is that your for loop isn't doing anything, you should write it like this:

``````for(int k=0; k < 100 ; ++k)
{
}``````

In this code, k < 100 is making the assumption that your k variable will never reasonably be 100 or more.

A quick fix of the code is:

``````#include <iostream>
#include <cmath>
#include <fstream>
#include <conio.h>

//it is better form to prepend std:: before cout
//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)
{

std::ofstream outfile; /*declare var to write to a file*/
outfile.open("hw4c.txt"); /*open output file hw4c*/
outfile<<"Ugonna Ibe" << std::endl;
outfile<<"hw4c" << std::endl << std::endl;

std::cout<<"Please enter the length of the column in inches> ";
std::cin>>length;

for(int k=1;;k++){
width=k*2;
area=width*width;
ratio=slender(length,width);
stress=compstress(area);

std::cout << std::endl << "Testing a beam with Area of " << width << " by " << width << " inches" << std::endl;

{
std::cout << std::endl << std::endl << "For a load of " << load << " pounds and a length of " << length << " inches," << std::endl << "recommended square beam has sides of " << width << " inches." << std::endl;
outfile << std::endl << "For a load of " << load << " pounds and a length of " << length << " inches, recommended square beam has sides of " << width << ".";

//end the loop
break;
}
else
{
std::cout << "Testing a beam with Area of " << width << " by " << width << " inches-Failed the tests" << std::endl;
outfile << std::endl << "Do not use " << width << " for a load of " << load << ".";
}
}

std::cout << "press any key to exit . . ." << std::endl;
_getch();

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);
}``````

This works for the test condition of 9000 and 120.

Sincerely,
Nate

Where you put

``for(int k=1;;k++)``

i do not need a second value? So I can just skip it so that it stops on its own without me telling the loop when to stop?

Your main issue, as hag++ mentioned, is that your for loop isn't doing anything, you should write it like this:

``````for(int k=0; k < 100 ; ++k)
{
}``````

In this code, k < 100 is making the assumption that your k variable will never reasonably be 100 or more.

A quick fix of the code is:

``````#include <iostream>
#include <cmath>
#include <fstream>
#include <conio.h>

//it is better form to prepend std:: before cout
//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)
{

std::ofstream outfile; /*declare var to write to a file*/
outfile.open("hw4c.txt"); /*open output file hw4c*/
outfile<<"Ugonna Ibe" << std::endl;
outfile<<"hw4c" << std::endl << std::endl;

std::cout<<"Please enter the length of the column in inches> ";
std::cin>>length;

for(int k=1;;k++){
width=k*2;
area=width*width;
ratio=slender(length,width);
stress=compstress(area);

std::cout << std::endl << "Testing a beam with Area of " << width << " by " << width << " inches" << std::endl;

{
std::cout << std::endl << std::endl << "For a load of " << load << " pounds and a length of " << length << " inches," << std::endl << "recommended square beam has sides of " << width << " inches." << std::endl;
outfile << std::endl << "For a load of " << load << " pounds and a length of " << length << " inches, recommended square beam has sides of " << width << ".";

//end the loop
break;
}
else
{
std::cout << "Testing a beam with Area of " << width << " by " << width << " inches-Failed the tests" << std::endl;
outfile << std::endl << "Do not use " << width << " for a load of " << load << ".";
}
}

std::cout << "press any key to exit . . ." << std::endl;
_getch();

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);
}``````

This works for the test condition of 9000 and 120.

Sincerely,
Nate

Where you put

``for(int k=1;;k++)``

i do not need a second value? So I can just skip it so that it stops on its own without me telling the loop when to stop?

This is an infinite loop. It will not stop on it's own. Somewhere in the loop you will have to `break;` out of it.

Oh okay. Thank you for your help!

This is an infinite loop. It will not stop on it's own. Somewhere in the loop you will have to `break;` out of it.

I should have mentioned the infinite loop in the code.

The way I changed the code, there is an assumption that at some point k will become big enough to satisfy your test condition and therefore dump you out of the loop with a break.

A much better practice would be to limit k to some reasonable value like this:

``for(int k=0;k<100;++k)``

In this example, you are asserting that no matter the inputs, it is never reasonable or expected that k will ever be 100 or larger. This limiting value could also be queried from the user:

``````while(0 != inch_max && inch_max < 1000)
{
``for(int k=0;k<inch_max;++k)``