Click here to Skip to main content
15,867,979 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.. I'm working on a c++ simulation that I'm creating and something bothers me so much and i couldn't solve it...
My Problem is that the IF and ELSE seems to not work some times because if it work correctly there must be no mistake...
this is my code :
C++
for (NC = 1; NC <= INN; NC++) {
		RRQLC = 0;
		for (RRC = 1; RRC <= INN; RRC++) {
			if (NC != RRC) {
				if (node[NC].x-node[RRC].x < node[NC].RADIO_RANGE && node[NC].x-node[RRC].x > node[NC].RADIO_RANGE-(node[NC].RADIO_RANGE*2)) {
					node[NC].RRQ++;
					node[NC].RRQL.push_back(RRC);
					if (abs(node[NC].x-node[RRC].x) > 0) {
						ENUM++;
						node[NC].E_NODE.push_back(node[RRC].id);
						SO << RRC << "\nNode " << node[NC].id << " E_NODE " << node[NC].E_NODE[RRQLC] << "\n";
					} else {
						ANUM++;
						node[NC].A_NODE.push_back(node[RRC].id);
						SO << RRC << "\nNode " << node[NC].id << " A_NODE " << node[NC].A_NODE[RRQLC] << "\n";
					}
					node[NC].SPACE.push_back(abs(node[NC].x-node[RRC].x));
					SO << "Node " << RRC << " is in RADIO_RANGE of node " << NC << "    #\nRRQL " << node[NC].RRQL[RRQLC] << "\n";
					RRQLC++;
					printf("node %d is in RADIO_RANGE of node %d\n",node[RRC].id,node[NC].id);
					//sqrt(pow((node[NC].x-node[RRC].x),2)+pow((node[NC].y-node[RRC].y),2));
				}
			}
		}
		if (node[NC].RRQ < 1) {
			NONE_NODE.push_back(node[NC].id);
			NBR++;
		}
		printf("%d\n",node[NC].RRQ);
	}


The innermost 'if' and the 'else' dose not work sometime because if it does it'll push back a number to E_NODE or A_NODE but none of it works correctly...and when i print it on a file it shows me wrong values because the 'if' and 'else' did not work and it didn't push back any value to the vectors and when the outermost FOR change the value that it prints does not exist then it shows some value of memory like 825241632
I'm talking about the E_NODE[RRQLC] and the A_NODE[RRQLC]
please help me to solve this problem if you have any idea...
Posted
Updated 31-Aug-15 21:51pm
v3
Comments
phil.o 1-Sep-15 2:31am    
Did you try to debug and see what is going on?
JENOVA_SHINRA 1-Sep-15 9:05am    
Yes i did but it had no warning about the code
phil.o 1-Sep-15 9:56am    
Debugging does not throw warnings about the code, it allows you to execute your code line-by-line and check the value of the variables.
JENOVA_SHINRA 1-Sep-15 10:36am    
My teacher says that as well but i still don't understand how to debug that shows every value and parameters of functions.
will you teach me how to debug as you said?
phil.o 1-Sep-15 10:46am    
You just have to put a breakpoint at the start of the code you are trying to debug, and launch the debug process by pressing F5 (in Visual Studio). From there you will be able to watch for any variable value (simply by putting the cursor on the variable name, or by putting a watch on the variable).
You can find a lot of information on your favorite search engine with the words "debugging c++" ; you will find plenty of tutorials and examples on debugging.
Debugging is an important field and should not be ignored by any developper.

You repeatedly forgot to take abs() on the .x difference terms (which presumable denote some kind of distance). I suggest you use a local variable to temporarily store that distance, so you don't have to rewrite the entire term so many times:
C++
for (NC...) {
   RRQLC=0;
   for (RRC...) {
      if (NC != RRC) {
         auto node_dist = abs(node[NC].x - node[RRC].x);

Also, that second if condition doesn't appear to make sense:
C++
if (node[NC].x-node[RRC].x < node[NC].RADIO_RANGE && node[NC].x-node[RRC].x > node[NC].RADIO_RANGE-(node[NC].RADIO_RANGE*2)) {

The right hand term calculates the difference from one term to twice the same term. I suspect you meant to reference two different terms here? (e. g. node[RRC].RADIO_RANGE rather than node[NC.RADIO_RANGE?)

I can't say what is wrong with the innermost if, because it is unclear what you expect. But I suggest if you just fix the above things first. May be it just works after that.
 
Share this answer
 
Comments
JENOVA_SHINRA 1-Sep-15 9:40am    
The second if will search for those nodes that are in the current node's radio range...
The radio range is set on 35 so each node's radio range will be from -35 to 35... Thats what it does.
Stefan_Lang 11-Sep-15 5:21am    
Sorry for the late response.

Please reread my solution, specifically the last part: That second if is nonsensical because you compare a distance value (which is presumably positive, and would be if you would use abs()) against a negative number, which is (presumably) always true (unless you have negative ranges). The last term that you compare against is
node[NC].RADIO_RANGE-(node[NC].RADIO_RANGE*2))
Again, you refer to the exact same number twice in this term, so the result is
-node[NC].RADIO_RANGE
which is presumably negative.

Excuse me for the lots of 'presumably', etc., but there are so many doubtful expressions and terms that I had to make a lot of educated guesses. As I said, you really need to fix that code, all of it!
Guys i figured out the problem of the code and i will share it with you.
but still it's so much weird and I'm still confused about it.
I did put an Equal sign in the innermost 'if' and it returned the value i needed.!!!
Can anyone tell why it does that?
C++
if (abs(node[NC].x-node[RRC].x) >= 0) {
 
Share this answer
 
Comments
Patrice T 1-Sep-15 10:26am    
Please, don't use a solution for this, you rather should Improve question and update your question with any thing new.
If the question is solved, note it in title or close the question.
If a solution was useful, you can also accept the solution.
JENOVA_SHINRA 1-Sep-15 10:38am    
OkSorry i just wanted that people see it.
I won't repeat again.
Check your array sizes and indexes. Your indexes are running from 1 to INN so the sizes must be at least INN+1.

With C/C++ arrays are indexed zero based. So it might be better to change the for loops to run from zero to INN-1 (end condition < INN).
 
Share this answer
 
Comments
JENOVA_SHINRA 1-Sep-15 9:43am    
I already know about that and i have created the class like so :

NODE node[INN+1]

My only problem is about the innermost if that doesn't work so perfect.
but thanks for your advice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900