For Loop Help

This is a discussion on For Loop Help within the C++ Programming forums, part of the General Programming Boards category; I am creating a program that calculates the beam sized needed for a certain load based on three given equation. ...

  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
    Attached Files Attached Files

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21