For loop adding to a variable - c++

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 ?

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.

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.

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.

Related

The for loop isn't entered even if the initial requirement is true

I have the following function with a for loop inside it. The code is run on an Arduino and the Serial.print function shows that the function is entered correctly with the correct input value. But the for loop isn't entered. Does anyone have an idea why?
void openvalveCold(int steps){
Serial.println(steps);
// Steps is confimed to be 200.
digitalWrite(sleep1,HIGH);
for (antalsteg = 0; antalsteg == steps; antalsteg++)
{
Serial.println("2");
//digitalWrite(dir1,HIGH);
digitalWrite(stepp1,HIGH);
delay(25);
digitalWrite(stepp1,LOW);
delay(25);
Serial.println(antalsteg);
nr_of_steps_cold++;
}
}
void loop{
// calling on function
openvalveCold(200);
}
A for loop is usually constructed like this:
for(init counter; condition; increase counter)
You have made the (false) assumption that it loops until the condition is true. That's wrong. It loops while it is true. Change to:
for (antalsteg = 0; antalsteg < steps; antalsteg++)
The loop isn't entered because the condition is false when the loop starts:
for (antalsteg = 0; antalsteg == steps; antalsteg++)
When the conditional of the loop is first evaluated, antalsteg is 0 and steps is 200. So antalsteg == steps evaluated to 0 == 200 which is false. So the loop is never entered.

C++ character variable value of '\x1'

I'm failing to understand why would the loop exit at the value of character variable i = '\x1'
#include <iostream>
using namespace std;
int main()
{
char i;
for (i = 1; i < 10, i++;)
{
cout << i << endl;
}
return 0;
}
Can somebody please explain this behavior ?
This is wrong
for (i = 1; i < 10, i++;)
/* ^ should be ; */
You only declared 3 regions for the loop, but put your increment statement in the middle area, and left your increment area empty. I have no idea which statement in the middle area your compiler will choose to execute. Best not to try to be cute and deceive your compiler. Let alone some colleague who will read your code years from now and go WTF???
A for loop has 3 distinct areas delimited by semi-colons:
The initialization area. You can declare as many variables in here as you want. These can be delimited by commas.
The test area. This is where an expression goes to test if the loop should continue.
The post loop area. This region of code gets executed after every loop.
Try to keep it simple. If it is going to be more complicated then use a while loop.
The reason that i ends up being 1 is that when i++ is zero, which terminates the loop, then i will become 1 (That is what the form of the ++ operator you used does). As the other answered have pointed out, once you fix your code by moving i++ out of the condition by replacing the comma with a semicolon, then i will make it all the way to 10 as desired.
for (i = 1; i < 10; i++)
You wrote for statement wrong.

While function doesn't work like I want it to

Had a new problem with the while function. As easy as it sounds, I still can't wrap my head around it.
Like my last program, this one closes unexpectedly after the correct and wrong messages.
I want this to loop after entering a number, so that the program won't stop.
Thanks for the help, if any.
#include <iostream>
using namespace std;
int main()
{
int X = 0; //setting the first variable
int num; //setting the second
while (X == 0) //this should happen whenever X is equal to 0
{
cout << "Type a number bigger than 3. "; //output
X++; //This should increase X, so that the next while function can happen
}
while (X == 1) //again, since I increased x by one, (0+1=1 obviously) this should happen
{
cin >> num; //standard input
if (num > 3) //if function: if num is bigger than three, then this should happen
{
cout << "Correct! Try again!" <<endl; //output
X--; //Here I'm decreasing x by one, since it was 1 before, now it becomes 0. This should make the "while (X == 0)" part happen again, so that another number bigger than three can be entered
}
if (num <= 3) //if function: if num is lesser than or equal to 3, this should happen
{
cout << "Wrong! Try again!" <<endl; //output
X--; //This is supposed to work like the "X--;" before, repeating the code from "while (X==0)"
}
}
}
now it becomes 0. This should make the "while (X == 0)" part happen again
Nope. While loops don't magically take effect at any point during execution of the program. You only enter a while loop when you've reached it from code above. Programs are executed top-to-bottom, generally.
You would need a loop around the entire program if you want to keep going round and round. Those whiles you have now should probably be ifs.
Merge the two while loops into one, while(true).
Put each previous while body into an if state with the clause from the old while in it.
while(true) {
if (X==0) {
// the X==0- case
} else if (X==1) {
// the X==1 case
}
}
in order to end your loop, do a break;.
You have to think of C++ programs as a sequence of instructions, like a recipe. while just means a loop: you check the condition. If true, you run the body. After running the body, you check only that condition again, and run the body if true. Whenever the condition is false at the start or end of the body of the while (the {} enclosed code after it), you end the loop and proceed to the next one.
The first loop runs, finishes, then the second loop runs in your code. Once the first loop exits, you do not go back into it just because the condition becomes true.
Understanding flow control is one of the "hard" steps of learning to program, so it is ok if you find this tricky.
There are many improvements you can do your code beyond getting it working -- there is, actually, little need for X at all. But baby steps! Once you get it working, you can ponder "how could I remove the variable X?".
Before making such fundamental changes to your program, you should get it working, and save a copy of it so you can "go back" to the last working version.
You want to wrap all that code in it's own while loop:
while (true /* or something */)
{
while (X == 0) //this should happen whenever X is equal to 0
{
// ...
}
At least put your second while loop inside the first one to get it working as intended. Otherwise your program has no reason to go back again.
Nevertheless it's not a good design.

Using cout inside an FOR loop

I tried testing the following code and found that the loop is never executed :
int i=0;
for(;i++;cout<<i)
{
if(i==5)
break;
}
I read the following post about the value returned by cout from the following post :
What's the difference between cout<<cout and cout<<&cout in c++?
But, I am unable to figure out why. Can someone help me with this.
int i = 0;
for (; i++; cout << i)
At the 1st loop, i++ is evaluated as 0 before increment happens and thus terminates the loop.
The first time the loop exit condition (i++) is checked, i's value is 0 (i.e. false). Hence it never enters the loop.
i++ is post increment. So i becomes 1 but the value which is checked in the loop exit condition is the value before increment - i.e. 0.

why this program is crashing

I wrote a small program in which, I want to set the value of a frame to 255 based on a vector:
result = cv::Mat::zeros(frame.size(),CV_8UC1);
std::vector<cv::Point2f> imageCorners;
.......................................................
for ( int i = 0 ; imageCorners.size();i++){
std::cout << imageCorners[i]<< std::endl;
result.at<uchar>(imageCorners[i]) = 255;
cv::imshow("result",result);
}
my question is: why the program crashes just after finishing the loop ?? even I see that result is correct ? the error message that I get is :
vector subscript out of range
for ( int i = 0 ; imageCorners.size();i++){
// ^^^^^^^^^^^^^^^^^^^
The underlined part is the condition. In this case you are saying "keep looping until the size of imageCorners is "false" (i.e. 0)". But you never change the size of the vector, so this condition never stops the loop, i keeps getting bigger, until you try to access an index that isn't actually in imageCorners.
Presumably you mean to loop until i gets bigger than the vector. Then use
for (int i=0; i < imageCorners.size(); ++i) {
This looks dodgy to me:
for ( int i = 0 ; imageCorners.size();i++){
you surely wanted to write something like:
for ( int i = 0 ; i < imageCorners.size();i++){
The condition of your loop, imageCorners.size() only yields the number of elements stored in the container. The statement will always evaluate to true as soon as you put one element into imageCorners. What you want is i < imageCorners.size().
for ( int i = 0 ; imageCorners.size();i++)
I think this loop will run forever if imageCorners.size() is different than 0. So when this
std::cout << imageCorners[i]<< std::endl;
gets executed, at some point i will be out of bounds and the program will crash.