problem with logic operators

so this program is meant to work as an Evolution simulator. each neuron is taken through some changing process, for now they are just added to, and then whichever Neuron ends up with the largest value will have that value substituted back in as the base value for all the other Neurons once the next Generation plays. This does not happen yet, as I am stuck trying to fix the problem explained below.



at the bottom of the code in NeuronPath, I am having a problem, for whatever reason, Neuron2 always executes, despite its condition in the if statement always being false. Also if you comment The if statement out, it will do the same thing with the next if statement.

ignore the Neuron2 = NeuralCarier. they are commented out for a reason.

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
65
66
67
68
69
70
71
72
#include "pch.h"
#include "AILogic.h"

#include <iostream>

//NeuronMain is where all functions meet up to build the program
void NeuralNet::NeuronMain()
{
	int n = 0;
	std::cout << "How many generations do you want to run for: ";
	std::cin >> n;

    int l = 0;

	for (int i = 0; i < n; ++i)
	{
		NeuronPath();
		
		std::cout << "Play next generation ";
		std::cin >> l;
	}
}
int main()
{
   
	NeuralNet UseDatSquishyBrain;
	UseDatSquishyBrain.NeuronMain();

}
//NeuronPath Basically controls the evolution of each Neuron, for now they are just added to an integer
//it also checks to find which Neuron has the largest value
void NeuralNet::NeuronPath()
{
	Neuron1 = Neuron1 + 1;
	Neuron2 = Neuron2 + 2;
	Neuron3 = Neuron3 + 3;
	Neuron4 = Neuron4 + 4;
	Neuron5 = Neuron5 + 5;

	if (Neuron1 == Neuron2 == Neuron3 == Neuron4 == Neuron5)
	{
     std::cout << "something has gone wrong ";
	}
	else if (Neuron1 > Neuron2 && Neuron3 && Neuron4 && Neuron5)
	{
		//Neuron1 = NeuralCarier;
		std::cout << Neuron1 << " ";
	}
	else if (Neuron2 > Neuron1 && Neuron3 && Neuron4 && Neuron5)
	{
		//Neuron2 = NeuralCarier;
		std::cout << Neuron2 << " ";
	}
	else if (Neuron3 > Neuron2 && Neuron1 && Neuron4 && Neuron5)
	{
		//Neuron3 = NeuralCarier;
		std::cout << Neuron3 << " ";
	}
	else if (Neuron4 > Neuron2 && Neuron3 && Neuron1 && Neuron5)
	{
		//Neuron4 = NeuralCarier;
		std::cout << Neuron4 << " ";
	}
	else if (Neuron5 > Neuron2 && Neuron3 && Neuron4 && Neuron1)
	{
		//Neuron5 = NeuralCarier;
		std::cout << Neuron5 << " ";
	}
	
}



Header file
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
  #pragma once

//Global_obj_ptr_var holds global info shared between all classes
class Global_obj_ptr_var 
{
public:
double NeuralCarier = 0;
//Neurons represent individuals. Their values can me thought of as levels. they all start at level 1
	double
		Neuron1 = 1,
		Neuron2 = 1,
		Neuron3 = 1,
		Neuron4 = 1,
		Neuron5 = 1;

};


class NeuralNet : public Global_obj_ptr_var
{
public:
void NeuronMain();
void NeuronPath();

};
Last edited on
else if (Neuron2 > Neuron1 && Neuron3 && Neuron4 && Neuron5)
{
//Neuron2 = NeuralCarier;
std::cout << Neuron2 << " is greater than boolean and of " << " ";
std::cout << Neuron1 << " ";
std::cout << Neuron3 << " ";
std::cout << Neuron4 << " ";
std::cout << Neuron5 << " ";
}

print and prove that it is executing incorrectly ...

what it says:
true if neuron 2 is > 1 or zero result of: (n1 !=0 && n3 !=0 && n4 !=0 && n5!=0)
else false. so if n2 is 2 or more, it will always be true. if n2 is 0 it will always be false. if n2 is 1 it depends. And so on.
Last edited on
Operator precedence:
1
2
3
Neuron2 > Neuron1 && Neuron3
// is same as
(Neuron2 > Neuron1) && Neuron3


1
2
3
4
5
6
7
8
9
if ( Neuron2 && Neuron1 > Neuron3 ) {
  // action
}
// is same as
if ( Neuron2 ) {
  if ( Neuron1 > Neuron3 ) {
    // action
  }
}
Yes, that is right. I reversed the order of && vs compare :( sorry.
sry jonnin could you better illustrate what you meant, what you wrote is a little hard to read.

and to prove that it is printing wrong, let me explain.
it is not printing wrong, what it is doing is right in the case that it should be executing, the problem is that I did not intend for it to do so.

also if this method I am using does not work, how else can I check to find which Neuron is the greatest.
Last edited on
Don't worry about what I said, I forgot that > is before && so it isnt right.

but your logic is still weird.
else if (Neuron2 > Neuron1 && Neuron3 && Neuron4 && Neuron5)

lets just start from scratch. what do YOU think that condition says / does? In english..


I think it says

IF Neuron2 is greater then Neuron1 AND is greater then Neuron3 AND is greater then Neuron4 AND is greater then Neuron5 THEN print Neuron2.


am I wrong.

In my mind it has to meet all of the conditions before it can execute.
Last edited on
the logic does not match what you said there.
the is greater does NOT carry through
you need to explicitly say that.

if n2> n1 && n2> n3 && n2>n4 && n2 >n5

what you wrote says if n2 > n1 and n3,n4, and n5 are not zero

c++ does not humanize logical statements. worse, it allows quite a bit of nonsense as legal.
if(x) means if x is not zero.
if (x > y && z) means if x > y and z is not zero.
if (x = y) says assign x the value of y and result to true if y was not zero. //youll get a warning here because its usually a typo for == ).

Last edited on
cpp is not a smart boi is he XD

this did fix the problem, thx.
Last edited on
Topic archived. No new replies allowed.