943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3135
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2006
0

If Else: Logic Jam?

Expand Post »
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;    
      

      // }
}
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 8th, 2006
1

Re: If Else: Logic Jam?

i believe your missing a close bracket "}" from your initial if block
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Nov 9th, 2006
0

Re: If Else: Logic Jam?

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
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 9th, 2006
0

Re: If Else: Logic Jam?

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.
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Nov 9th, 2006
0

Re: If Else: Logic Jam?

Here is a link that might help you.

http://www.functionx.com/managedcpp/keywords/static.htm
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Nov 9th, 2006
1

Re: If Else: Logic Jam?

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...
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005
Nov 9th, 2006
0

Re: If Else: Logic Jam?

Thank-you for all you help and suggestions
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 9th, 2006
1

Re: If Else: Logic Jam?

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:
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Nov 9th, 2006
0

Re: If Else: Logic Jam?

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
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 10th, 2006
0

Re: If Else: Logic Jam?

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.
Reputation Points: 36
Solved Threads: 2
Junior Poster in Training
nanodano is offline Offline
78 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Recursion II
Next Thread in C++ Forum Timeline: edit C++ window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC