Thread: For Loop Help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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

  2. #2
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    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?
    Last edited by ManyTimes; 03-22-2010 at 05:15 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    I need to use the format like I have, right now my for loop is infinite and I'm not sure why

  6. #6
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    by the way, i think there is a duplicate post of this topic.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, continued here - For Loop Problems
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed