The error here is that the number never stops increasing (0 -> infinite). Why doesn't the loop stop once totalCareTime hits 25?
int totalCareTime = 0;
while (totalCareTime <= 25 || interrupted == false)
{
++totalCareTime;
cout << totalCareTime << endl;
if (time == time + emergencySam || time == time + emergencySid)
{
interrupted = true;
}
}
You use ||, so as long as either condition is true, the loop continues. If interrupted stays false (which will always be the case unless emergencySam or emergencySid have a value of 0; it's unclear how they are set or whether they can be changed at all), the loop will go forever. Change to && if you need both conditions to be true for the loop to keep going.
Related
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.
I have a problem with the while loop, I have instructed that the key1 variable should only be executed when at least some "if" instructions have been executed, however, it re-executes the while loop even if the variable key1 is equal to 0.
It should be noted that I do not change the value of the key1 variable in another part of the function
void form_table(...) {
int key1=1;
while (key1 != 0) <==(2)
{
key1=0;
if (dx->get_numero() == 1 && dy->get_numero() == 1)
{
key1++;
//Some code
}
else if (dx->get_numero() == 1 && dy->get_numero() == 0)
{
key1++;
//some code
}
else if (dx->get_numero() == 0 && dy->get_numero() == 1)
{
key1++;
//some code
} <==(1)
else
break;//I put it in case, but even with that it goes back into the buble while
}
}//Here is the problem, when finish the funcion execution the program comes back to the line (1), and then re-runs the while cycle (2)
the loop works if key1 is different from 0 if you increment key1 and therefore it is different from 0 the cycle reinitiates.
you have to do:
while (key1 == 0) {
// rest of the code
}
are following code samples equivalent?
This:
while (true)
if (!a[counter] || !b[counter++]) break;
and this:
while (true){
if (!a[counter] || !b[counter]) break;
counter++;
}
i mean, would increment be performed after all conditions' checking done?
Here:
int _strCmp(char* s1,char*s2)
{
int counter = 0;
while (s1[counter]==s2[counter])
if (!s1[counter] || !s2[counter++]) return 0;
if (s1[counter] > s2[counter])
return 1;
if (s1[counter] < s2[counter])
return-1;
return 0;
}
Are there some cases, when this function doesnt work correctly?
No they are not.
Here if !a[counter] returns true the OR'ed condition will not be evaluated.
The second condition in OR is only evaluated if the first condition is false. This is because anything OR'ed with true will be true.
Look at the following image :
As in the image you can see that case 2 is not equivalent
Since the it is incremented post-evaluation (rather than ++counter), the value that will be returned is the value before it is incremented. So, those are equivalent statements.
If counter = 6, then !b[counter++] will return b[6], and then increment 6 to 7.
You could try it yourself changing your code to this:
run = 5;
while (run > 0) {
run--;
if (!a[counterA] || !b[counterA++]) break;
}
run = 5;
while (run > 0){
run--;
if (!a[counterB] || !b[counterB]) break;
counter++;
}
// compare counterA and counterB
EDIT:
Regarding "i mean, would increment be performed after all conditions' checking done?"
No. There are post and preincrement operations. Since you are doing a postincrementation your value would be incremented after it's value was used to evaluate the expression.
I am experiencing troubles with what I suspect to be while loops. I am working on building a simple game, and I think my two while loops are interfering with eachother somehow. here's the main function! Thanks in Advance!:
int main( int argc, char* argv[])
{
SDL_Startup();
while(Playing == false && quit == false)
{
StartingScreen.input();
StartingScreen.render();
character.input();
SDL_Flip( screen );
SDL_Delay(1000/FPS);
}
while(Playing == true && quit == false)
{
CAMERAGUY.Camera();
character.input();
character.adjust();
SuperSnail.move();
SuperSnail.attack();
TheWall.boundaries();
TheWall.render();
SuperSnail.render();
character.render();
character.reset();
HUD.render();
SDL_Flip(screen);
SDL_Delay(1000/FPS);
cout << StartingScreen.x << endl;
}
if(Playing == false)
cout << "Playing == false" << endl;
if(quit == true)
return 0;
}
So the bool playing is set to false to begin with, and is set to true when my character runs out of lives. so when Playing is set to false in the second loop, it doesn't repeat the first loop. I JUST thought, I think maybe it would work if I put the two loops in a separate loop.
I've edited your code with comments to explain what it is doing (and why it is not behaving as you expect):
int main( int argc, char* argv[])
{
SDL_Startup();
// Set `Playing` to false
while(Playing == false && quit == false)
{
// Do stuff which applies when `Playing` is false
//
// At some point, set `Playing` to true, so that
// the loop ends
}
while(Playing == true && quit == false)
{
// Now do stuff which applies when `Playing` is true
//
// At some point, set `Playing` to false, so that
// the loop ends
}
// Both loops have come to an end, and there
// is no code to return to the first loop
// so execution continues below:
// This line always executes because `Playing` is always false
// by this point:
if(Playing == false)
cout << "Playing == false" << endl;
// This conditional statement doesn't do what you think, because
// in the case that `quit == false`, the end of your `main` function
// is reached and returns 0 by default, meaning that the outcome
// is the same no matter what the value of `quit`
if(quit == true)
return 0;
}
The solution: enclose the both loops in a while(quit == false) loop. This means that both when the second loop completes, the first loop will be evaluated again, until you are ready to stop execution by setting quit to true.
A few other tips:
A neater way of expressing quit == false is !quit.
A neater way of expressing Playing == true is Playing.
Using global variables in the way that you are is almost certainly a bad idea, and you should probably rethink your design.
It is pretty simple. In your current implementation you will never return to your first while loop if your second loop is finished, because you reach the return of your main routine.
I guess you should always use only one main loop for your game.
I am trying to increment a lap counter in my game by one but because I have to put this code in the game loop my counter goes over every time by about 500 instead or moving up one. Here is my code. The checkpointPassed variable is only true when a checkpoint is passed through. I know this works and the checkpoint number is the current checkpoint and they start at 0.
if(checkpointNumber == 0 && checkpointPassed == true)
{
lapNumber += 1;
}
I can't post the game loop because it is quite large.
Any Help is appreciated.
EDIT
Here is some more of the code so you can see what I am trying to do.
if(distance > carRadius && markerCounter < 5000)
{
if(checkpointPassed == true)
{
markerCounter++;
}
}
if(checkpointNumber == 0 && checkpointPassed == true)
{
lapNumber += 1;
}
if(distance < carRadius)
{
markerCounter++;
cross->SetX(checkpointX);
cross->SetY(checkpointY);
cross->SetZ(checkpointZ);
checkpointNumber += 1;
checkpointPassed = true;
}
if(markerCounter > 4999)
{
checkpointPassed = false;
cross->SetPosition(0,-50,0);
markerCounter = 0;
}
Add another two variable called inCheckpoint, which stores whether the user is currently "inside" the checkpoint or not. This allows you to detect when the user enters a checkpoint and only increment the lapNumber then. The code would look as follows:
if(checkpointNumber == 0 && checkpointPassed == true)
{
if (inCheckpoint == false) /* previously not inside a checkpoint */
lapNumber += 1;
inCheckpoint = true;
}
else
{
inCheckpoint = false;
}
UPDATE: Don't rely on checkpointPassed:
if(distance < carRadius)
{
if (inCheckpoint == false) /* previously not inside a checkpoint */
lapNumber += 1;
inCheckpoint = true;
}
else
{
inCheckpoint = false;
}
You could set/pass a gueard value that indicates how many iterations in the game loop you are (or whether this is the first iteration). If it is the first iteration (within the current lap), increment the variable as you do now, otherwise don't
You will need to reset this guard value for each lap -- e.g. right after you increment lapNumber.
You might need to cancel the 'checkpointPassed` state.
if (checkpointNumber == 0 && checkpointPassed == true)
{
lapNumber += 1;
checkpointPassed = false;
}
This means that you won't be counting the lap again until the next time a checkpoint is passed, which is presumably when you need it counted.
However, if you need checkpointPassed true later in the loop, then you'll need to think whether you need yet another variable, such as lapCounted, which is set to false when checkpointPassed is set to true, and reset to true by the code above (instead of setting checkpointPassed, not as well as setting it).
If I understand correctly what you said, your 'if' statement is inside the main loop and when you pass a checkpoint, 'checkpointPassed' becomes true. For how long?
If it stays 'true' for a few iterations, then each time your game loop does an iteration,your lap counter is incremented. In this case, you should either set checkPointPassed to false at the end of the iteration, or use a different variable, that you set to true at the same time that checkPointPassed becomes true and false after incrementing.
If this does not answer your question, can you give a little more context as with only this part of the code, it is hard to figure out what you want to do.