Bool Flag not breaking out of loop

Hello again everyone! I have one more question. I have a function here which is not busting out of the while loop when the flag is set to false. Any point in the right direction of where my logic is wrong would be great. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#include "structs.h"


bool checkLAR2(char name[32], float num1, float num2, float num3, char eol[16])
{
	bool flag = true;
	do
	{
		if (strcmp(name, lar2Name) == 0)
		{
			flag = true;
		}
		else
		{
			flag = false;
		}

		if ((num1 == NULL) || (num1 < 0.0) || (num1 > 60.0))
		{
			flag = false; // this should be setting flag = false and breaking out of loop but it doesnt break the loop
		}
		else
		{
			flag = true;
			
		}
		if (num2 == NULL || num2 < 5.0 || num2 > 90.0)
		{
			flag = false;
		}
		else
		{
			flag = true;
		}

		if (num3 == NULL || num3 < -90.0 || num3 > 180.0)
		{
			flag = false;
		}
		else
		{
			flag = true;
		}

		if (strcmp(eol, lar2Eol) == 0)
		{
			flag = true;
			break;
		}
		else
		{
			flag = false;
		}

	} while (flag != false);

	return flag;
}
num 1 is not a pointer. Why do you check it for null?

what is the input when it fails?

I think it might work if you just put the keyword break in all the false conditions.
do you WANT it to be able to set to true after having set to false, or set to false after having set to true?? As it stands, it can overwrite the flag a few times and then get to the while's test.

what exactly are you trying to do. It looks like some kind of angle checks.
Last edited on
By null I mean to just check for any value. even if it is zero that is fine as long as there is something. Will null work for this?

Now looking at it a little closer I think it is actually running correctly

The input is l2_a2_r2,0.0,0.0,0.0,,,,,,,,,
Last edited on
this should be setting flag = false and breaking out of loop but it doesnt break the loop

Why would it magically break out of the loop? There's still more code after it that might set flag to true.

Your code is very strange. Why is there a loop at all?

And floats are not pointers and therefore should not be compared to NULL (and you shouldn't use NULL in C++ anyway; use nullptr or, if you must, 0).

It looks like you might mean something like this:

1
2
3
4
5
6
7
8
bool checkLAR2(char name[32], float num1, float num2, float num3, char eol[16])
{
    return strcmp(name, lar2Name) == 0 
        && !(num1 <   0.0 || num1 >  60.0)
        && !(num2 <   5.0 || num2 >  90.0)
        && !(num3 < -90.0 || num3 > 180.0)
        && (strcmp(eol, lar2Eol) == 0);
}

Last edited on
It wouldn't "magically" break out of the loop. it will break out of the loop when flag is set to false. hence the while(flag != false)

i will need to read up on NULL clearly i misunderstood the use of that.
It wouldn't "magically" break out of the loop.

That's my point. You are expecting it to magically break out of the loop even though after that line THERE ARE OTHER LINES OF CODE THAT MAY CHANGE YOUR LITTLE FLAG TO SOMETHING ELSE.
Is that really so hard to understand?
And what about the insane loop?
Wake up!

Last edited on
no, that is not what null is for.
all types have a value all the time. that value may be zero (which is all null is, its a constant that == 0). You can't make something without a value -- as soon as it exists, it has bytes in memory and bytes in memory must have values, as they are actually physical circuits that exist in some state, and every state is a value (some values are invalid, but its still a value that you could force-print in hex).

if this is an overwriting logic that is doing the right thing, its fine.. but we don't really know what you are doing. If its not supposed to overwrite once a value is set, its very wrong. Which way did you intend? Regardless I would rewrite it so that it has a default value (you did that, its true) and then set it to false under some condition. If it needs to go back to true under other conditions, exclude those from setting it false logic.

Last edited on
There is no need to be so critical. For someone that is new to programming it might not be obvious that the loop condition in a do-while loop is only checked at the end of each iteration, and not between each expression.
Last edited on
There is no need to be so critical.

He does it because he enjoys it. He clearly loves humiliating people, bullying them and making them feel bad. Especially people who are new to coding, and maybe feeling a bit nervous and in need of help - because, after all, people like that are the easiest to bully, right?
Last edited on
Topic archived. No new replies allowed.