I have come to point in my program where I have decided a static variable would be the best option. I am not very familiar with static variable use and have never implemented them often.

The area of concern is highlighted in red: I am simply using a time function: I need to survey the system clock once and store this variable permanently for further mathematical use when\ if the function re-runs (user's option). The area of viewing and grabbing the first system time request (into a variable) for future use is the problem area.

I have tried static (Look.cpp) and it seems to not work properly if at all. I am probably using it incorrectly.

I simply wish to store this first system time view for future use. I have been researching the problem but I am still not quite sure how this is truly supposed to work (even though it appears so simple).

"The static keyword allows a variable to maintain its value among different function calls. If the value of a static variable changes when the variable has been accessed, the variable keeps the new value. If the same variable gets accessed again, it would be holding its most recent value. "

Thank-you in advance for any help.

sharky_machine


Look.h

class Look {
public:
float shipPos ;  
float satPos; 
float interior; 

void printShipPos(); // new function declaration

Look(); 
~Look(); 
};

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 Marmalade-- Please Enter a Choice:"<< endl;
    std::cout << ""<< endl;
    std::cout << "[1]Check Status for Transmission to Base"<< endl;
    std::cout << "[2]Exit Interface Program"<< endl;
    std::cout << ""<< endl;
    std::cout << "--------------------------------"<< endl;

    cin >> choice;

    Look look1; 

    if(choice == 1){
        look1.printShipPos();
             
    }
    else {
            x=1;
    }
}  
return 0;
}

Look.cpp

#include "Look.h"
using namespace std;

long currPos = 0;
int checkFlag = 0;
static int start;                                    
int next = 0;


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

void Look::printShipPos() 
{ 
    { 
        
        int shipPos = 228; // default start position (pos 0)

                    
                   [B] //1st time taken        
                    clock_t start;
                    start=time(NULL);
                    cout << start<<" 1st TIME"<<endl;[/B]

if (checkFlag > 0) {

                    //2nd time taken
                    clock_t next;
                    next=time(NULL);
                    cout << next<<" 2nd TIME" <<endl;

                    currPos= (next-start);
                    //currPos = shipPos;
                    cout << currPos <<" currPos"<<endl;
                    //cout << shipPos <<" shipPos"<<endl;
}
else {shipPos = 228;}

    }


// 3.80 minutes (228 seconds) per segment of Fly-Over

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


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

return;    

}

Solved-- I think.

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.