Reputation: 781
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 ?
Upvotes: 0
Views: 217
Reputation: 3
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.
Upvotes: 0
Reputation: 6260
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.
Upvotes: 2
Reputation: 23634
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.
Upvotes: 7