If Else: Logic Jam?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

If Else: Logic Jam?

 
0
  #1
Nov 8th, 2006
Hello:

Something as seemingly simple as an If\ Else statement is causing me much trouble and I cannot figure out where the error lies. I believe it to be in:
  • The variable scope
  • The use of the static variable
  • Or, the misplacement of curly brackets (something that has a tendency to trip me up at times)
Also, while debugging, the variable of "shipPos" should be 228 at different points (and it couts as this) but the debugger refers to the value as "-1.0737418e+008" This honestly does not make sense to me-- I assumed it to be the memory location (?) but why would that be in this case?

Any hints, help, or shove in the right direction would be surely appreciated.

Regards
sharky_machine

#include <ctime> 
#include <cstdlib> 
#include <iostream>
#include "Look.h"
using namespace std;

int checkFlag ;                            
int* pTest; 

Look::Look() {
}
 
Look::~Look() {
}

void Look::printShipPos() 
{ 
        if (checkFlag > 0) {
        
static int shipPos = 0;

//-------------------------Pointer Test
//pTest = &shipPos;
//cout << pTest << endl;
//cout << *pTest << endl; // Works!
//-------------------------Pointer Test

        cout << shipPos << "_shipPos 1"<<endl;
        shipPos = shipPos + 228;
        cout << shipPos << "_shipPos added"<< endl;
                           }

        //{   
               //RECHECK all values below


if (shipPos == 228) {
std::cout << "French Polynesia [Out of Transmission Range]"<<endl; 
else if (shipPos == 456){
  std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl; 
}


/* else if ((shipPos >= 456) && (shipPos < 684)){
   std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl; 
}
else if ((shipPos >= 684) && (shipPos < 912)) {
   std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl; 
} */

.
.
.


//---------------------counter for fly-over access
checkFlag++; 
cout <<checkFlag<< endl;

return;    
      

      // }
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: If Else: Logic Jam?

 
1
  #2
Nov 8th, 2006
i believe your missing a close bracket "}" from your initial if block
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: If Else: Logic Jam?

 
0
  #3
Nov 9th, 2006
Originally Posted by TylerSBreton View Post
i believe your missing a close bracket "}" from your initial if block
Yes, Thank-you for pointing this out, but I think my problems goes deeper somehow .

I appreciate your help.
Regards,
sharky_machine
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: If Else: Logic Jam?

 
0
  #4
Nov 9th, 2006
I think the problem is because when you declare a variable static it affects the lifetime of it. You declare it inside the If brackets { }, so it will be destroyed at the end of the if statement. But, the static variable probably doesn't want to go away. It probably gets filled with information that really is another variable or a pointer, but your debugger still watches that variable as shipPos. Then, every time that function is called, it creates a new shipPos. My knowledge of compilers only goes so far, but I think that might confuse it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: If Else: Logic Jam?

 
0
  #5
Nov 9th, 2006
Here is a link that might help you.

http://www.functionx.com/managedcpp/keywords/static.htm
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: If Else: Logic Jam?

 
1
  #6
Nov 9th, 2006
I noticed too that you declare shippos inside an if block { } and try to refer to it in the next if statement, which is in a different block of code. That is probably why it prints out as 0+228 correctly, but then after that block, it is released. Your compiler might give more precedence to { } blocks than to the static variables. Compiler people trying to protect from memory leaks...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: If Else: Logic Jam?

 
0
  #7
Nov 9th, 2006
Thank-you for all you help and suggestions
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: If Else: Logic Jam?

 
1
  #8
Nov 9th, 2006
One more suggestion -- format your code properly and wayward braces will no longer be a problem. Don't indent 3 tabs then next line 1 tab, then 2 tabs.

Your first brace should be far left and every line after indented 4 spaces. Each and every { indent 4 more spaces sterting with the next line. When you get to a } unindent 4 spaces, then add the } on a line by itself.

Then your code is easier to read:
  1. void Look::printShipPos()
  2. {
  3. if (checkFlag > 0)
  4. {
  5. static int shipPos = 0;
  6.  
  7. cout << shipPos << "_shipPos 1"<<endl;
  8. shipPos = shipPos + 228;
  9. cout << shipPos << "_shipPos added"<< endl;
  10. }
  11.  
  12.  
  13. if (shipPos == 228)
  14. {
  15. std::cout << "French Polynesia [Out of Transmission Range]"<<endl;
  16. else if (shipPos == 456){
  17. std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
  18. }
  19.  
  20. //---------------------counter for fly-over access
  21. checkFlag++;
  22. cout <<checkFlag<< endl;
  23.  
  24. return;
  25.  
  26. }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: If Else: Logic Jam?

 
0
  #9
Nov 9th, 2006
Nanodano & WaltP:

Thank-you again for all of your help and suggestions; I followed everything you told me of and the problem I have been having is fixed.
I appreciate it a lot. Solved.


sharkey_machine
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: If Else: Logic Jam?

 
0
  #10
Nov 10th, 2006
hey, would you mind posting your code of sending me your code? I'd like to see your working code just so I can study it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC