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; 
} */

[B].
.
.[/B]


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

return;    
      

      // }
}

i believe your missing a close bracket "}" from your initial if block

commented: thanks for helping :) +1

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 :confused:.

I appreciate your help.
Regards,
sharky_machine

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.

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...

Thank-you for all you help and suggestions :)

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:

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

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


    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;
    }

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

    return;    
      
}
commented: TY :) +1

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

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.

nanadano:

Here is the code you requested. Thank-you for your interest.

Upon fixing the previous error that we discussed, another bug has appeared that I find very odd: It seems that when I run the program at first and enter a choice via the user interface (in this case "1" as it's the only valid choice at this point besides "2" which is simply used to exit) it takes the input and loops directly back to the interface requesting user input again; after entering "1" a second time it finally runs as it should looking at ship position (shipPos). Again, At this point it requires me to enter user input twice to run the initial shipPos. I have been debugging and stepping-through the entire thing but have yet to find this bug.

Here is the code:

Look.h

class Look {
public:
 

void printShipPos(); // new function declaration

Look(); 
~Look(); 
};

look_Earth.cpp

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

int checkFlag ;                            
 

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

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

        //cout << shipPos << "_shipPos_1"<<endl;
        shipPos = shipPos + 228;
        //cout << shipPos << "_shipPos added"<< endl;
    
    
        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 == 684){
        std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl; 
        }
        else if  (shipPos == 912) {
        std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl; 
        }
        else if (shipPos == 1140){
        std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;     
        }
        else if (shipPos == 1368){
        std::cout << "Gulf of Mexico [In Transmission Range]"<< endl; 
        }
        else if (shipPos == 1596){
        std::cout << "Tampa, Florida [In Transmission Range]"<< endl; 
        }
        else if (shipPos == 1824){
        std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl; 
        }
        else if (shipPos == 2052){
        std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl; 
        }
        else if (shipPos == 2280) {
        std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl; 
        }
        else if (shipPos == 2508){
        std::cout << "Canary Islands [Out of Transmission Range]"<< endl; 
        }
        else if (shipPos == 2736) {
        std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl; 
        }
        else if (shipPos == 2964) {
        std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;     
        }
        else if (shipPos == 3192){
        std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;         
        }
        else if (shipPos == 3420) {
        std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;     
        }
        else if (shipPos == 3648) {
        std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;     
        }
        else if (shipPos == 3876) {
        std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl; 
        }
        else if (shipPos == 4104) {
        std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;         
        }
        else if (shipPos == 4332) {
        std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;     
        }
        else if (shipPos == 4560) {
        std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl; 
        }
        else if (shipPos == 4788) {
        std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl;  
        }
        else if (shipPos == 5016) {
        std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl;  
        }
        else if (shipPos == 5244) {
        std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl; 
        }
        else if    (shipPos == 5244){
        std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl;  
    }
}
    checkFlag++; 
    //cout <<checkFlag<< endl;

    return;    
    
}

Main.cpp

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


int choice = 0;

int main()
{
int x = 0;     

    while ( x == 0 ) 
    {
    std::cout << "________________________________"<< endl;
    std::cout << "________________________________"<< endl;
    std::cout << ""<< endl;
    std::cout << "Welcome to Station VIREO -- Please Enter a Choice:"<< endl;
    std::cout << ""<< endl;
    std::cout << "[1]Check Status for Transmission to Base"<< endl;
    //std::cout << "[1]Check Client Satellite Position(s)"<< endl;
    std::cout << "[2]Exit Interface Program"<< endl;
    std::cout << ""<< endl;
    std::cout << "--------------------------------"<< endl;

    cin >> choice;
    
Look look1; 

        if(choice == 1){
        look1.printShipPos();         
        }
        //else if(choice == 2){
        //cout<<"You Chose 'Satellite View'"<<endl;         
        //}

        //else if(choice == 3){
        //cout<<"You Chose To Check Station Life Support Status"<<endl;         
        //}


        else {
                x=1;
             }
    }  
return 0;
}

Hey,
In your printshippos function, you declare checkFlag at the top of that cpp file. When you initialize it like that w/o any number, it is either an unknown number, or in most cases, 0. The first time you call printshippos your checkflag is 0. 0 > 0 is a false statement, so your entire function is not executed. You can solve it by doing any of these

  • putting checkflag++ at the beginning of the function
  • setting test condition in printshippos to checkflag >= 0
  • calling printshippos one time at the very beginning of main(not recommended)

nanodano:

TY so much for your reply. I followed your suggestion(s) and used:

* setting test condition in printshippos to checkflag >= 0


That was the solution :cheesy:

Thanks again. Appreciate it.

sharky_machine

Hey,When you initialize it like that w/o any number, it is either an unknown number, or in most cases, 0.

Uninitialized is uninitialized, and always holds a junk value with the exception of global variables which are automatically initialized to 0.

Sharky, to avoid such subtle bugs *always* initizlize your variables when declaring it.

s.o.s.

Thanks for the clarification. I noticed that his was default initialized to 0 so i thought maybe it depended on the compiler. Now I know it only does that with global variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.