0

I am trying to create a loop that adds 1 to a int variable every time the if statement is true

But while testing the code even though the if statement is true the variable is not incremented, as if the my for loop is not incremented at all....

Code sample:

int left_jab_count;
if(area >=100000 && area1 <100000)
{
    cout<<"LEFT JAB HAS BEEN THROWN"" "<<area<<endl;

    for(int left_jab_count = 0; left_jab_count < 0 ;++left_jab_count)
    {
        cout<<"Left Jab :"<<left_jab_count<<endl;
    }
}

can anybody see where am going wrong here ?

2
  • 4
    Why are you declaring left_jab_count twice? Commented Apr 18, 2013 at 0:21
  • 3
    Also, get in the habit of using SPACEBAR Commented Apr 18, 2013 at 0:24

3 Answers 3

7
 for(int left_jab_count = 0; left_jab_count < 0 ;++left_jab_count)
                                        //^^^^left_jab_count is never < 0
                                        // change <0 to some value larger than 0

you for loop is never executed. Therefore, left_jab_count will never get incremented, you never enter the body of for loop.

Meanwhile, you declared left_jab_count twice.

Sign up to request clarification or add additional context in comments.

7 Comments

And declaring left_jab_count twice means that the one declared in the for loop hides the other one, but only while in the for loop.
@MikeDeSimone yes, agreed.
thx for your replay...... I have tried to change the middle bit of my for loop as suggested to this : int left_jab_count = 0; if(area >=100000 && area1 <100000) { cout<<"LEFT JAB HAS BEEN THROWN"" "<<area<<endl; for(int left_jab_count = 0; left_jab_count < 1 ;++left_jab_count) { cout<<"Left Jab :"<<left_jab_count<<endl; } }
but the the Left Jab_count always equals to 0
@Tomazi if this is the case, then you should look at JBently's post, it does what you want. I should not repeat what JBently has already answered.
|
2

tacp has adequately covered the issues with your current code, so I won't go into those. Based on your specification, "I am trying to create a loop that adds 1 to a int variable every time the if statement is true", what you want is something like this:

int left_jab_count = 0; // Don't forget to initialise this

while (true) // Replace with real loop
{
    // Do NOT initialise left_jab_count here, otherwise it will be
    // reset to 0 on every loop

    // Insert code which changes area and area1

    if (area >= 100000 && area1 < 100000)
    {
        cout << "LEFT JAB HAS BEEN THROWN " << area << endl;
        left_jab_count++;
    }
}

Unless you've misstated your specification, then you don't need a for loop inside the if block.

8 Comments

I have also tried your solution but the variable is only incremented once.....in second instance or third etc. nothing happens
@Tomazi Then you need to re-state your problem, because according to you, incrementing only once is exactly the behaviour you want - "adds 1 to a int variable every time the if statement is true". You need to re-enter the if block multiple times for it to increment multiple times.
I know and i do enter the if statement more than once and it is always 1
@Tomazi Ok, well in that case it sounds to me like maybe you're including int left_jab_count = 0 inside the loop where the if block resides. Place it before any the block where all of that logic lies.
@Tomazi If you're still stuck after this, please post the entire relevant code as a new question.
|
0

JBentley answered the question which was set by you. In the While statement that he posted you should add a condition which is true so that the code inside can run. I guess you want to enter:

while (left_jab_count < NUMBER)

Be sure to have a true condition so that the loop can start and run the if statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.