-
For Loop Help
I am creating a program that calculates the beam sized needed for a certain load based on three given equation. I am having trouble with my for loop. I want the program to continue to test the beam width in increments until the if test is satisfied, but it seems as though it the for loop is not working correctly and I am not sure why. Any suggestions??? I attached the code
-
Well...
A for loop looks like this (or better said; it can look like this)
Code:
for(int i = 0; i < 10; i ++)
{
cout << "Value of integer variable i: " << i;
}
If you want to do a thing, at least once, you should use a do-while loop, looks like this:
Code:
int i = 0
do
{ //This is ran at least one time no matter what the "while statement" below returns
cout << "ManyTimes is such a handsome guy";
i++;
}
while(i < 10);
Here is a main you might like:
Code:
int main()
{
double load,length,loadb,width(6),test,loadc,result;
ofstream outfile; //declare var to write to file
outfile.open("hw4c.txt");//open output file to write to
char c;
do
{
cout<<"Enter the expected load in pounds: ";
cin>>load;
cout<<"Enter the length of the column in inches: ";
cin>>length;
{
loadb=buck(length,width);
loadc=comp(width);
result=slender(length,width);
}
cout<<loadb<<"\n";
cout<<loadc<<"\n";
cout<<result<<"\n";
if (load<=loadb && load<=loadc && result==1)
{
//test=0;
cout<<"For a load of "<<load<<" and a length of "<<length<<" inches, recommended sqaure beam has a side of "<<width<<" inches\n";
}
else
test=1;
cout<<test<<"\n";
cout << "Want to quit or go again? (q = quit, anything else continue";
cin >> c;
}while(c != 'q'); //programme runs as long as "c" is not "q" (little q)
system("pause");
return 0;
}
With that code in the "int main()" function, your program will run over and over again, as long as the user doesnt write "q" at the end when he is asked for quit or continue... :)
PS: I think Ive misunderstood your question, you dont want Main to loop? :)
hmm...
//for(width=2;test=1;width=width+2)
What are you trying to loop? How many times should the loop go? What? :D
-
I probably should have been more clear in my problem statement. I need to automate the selection of beam width. Which means I have to run the functions until they pass the test. I am incrementing the width by 2.(2x2 4x4....) I want my For loop to run until the if test is true and the variable test=0 and print out the statement
-
Something like
Code:
for ( width = 2 ; width < 1024 ; width += 2 ) {
ok = somecalculation();
if ( ok ) break;
}
if ( !ok ) {
// no beam < 1024 would work
} else {
// width works
}
-
I need to use the format like I have, right now my for loop is infinite and I'm not sure why
-
by the way, i think there is a duplicate post of this topic.
-