I need help with manipulating my computers clock through a c++ program and i have no idea on how to make the program. Does anyone have any idea on how to do this?

Recommended Answers

All 16 Replies

What kind of manipulation are you looking for? Standard C++ pretty much only gives you query capabilities for the clock, though you have comprehensive time and date control.

well i want it so that i can take it and subtract a chosen amount of time from the clock.

To what end? You can easily call std::clock() to get the current value and perform math on it. But the result may not be meaningful.

like take the current time and subtract say 6 hours. This is all for a timezone project im doing and i have everything but the clock

That's slightly harder, but you have more control and it's actually portable:

#include <ctime>
#include <iostream>

int main()
{
  std::time_t now = std::time ( 0 );
  std::tm *local = std::localtime ( &now );

  local->tm_hour -= 6;

  std::time_t before = std::mktime ( local );

  std::cout<<"Now:         "<< ctime ( &now ) <<'\n';
  std::cout<<"6 hours ago: "<< ctime ( &before ) <<'\n';
}

C++ supports something called a clock, and means system clock ticks, not wallclock time. Keep that in mind when asking questions about the "clock", because what you actually want to manipulate is date/time.

thanks ill try it out

While I'm no expert at C++ I think I can help you with your dilema.

I use the library time.h so you'd want to add

#include <time.h>

to your header files to use this. I saw an earlier post uses ctime but anyway

if you use time.h you'll be able to use things like this:
this is all assuming that you have using namespace std; in there as well, although I'm not sure that time.h requires it.

Anyway the main two things you want to use are clock() and clock_t

clock_t is a type. it is a variable type like int to store clock variables.
clock() returns the given system clock at a time. Now this number is some strange number that won't really make any sense to you, so you just have to work with it in the way that I explain.


Let's say you want to have something happen 5 seconds in the future. Here's what you do.

clock_t FiveSecondsLater; // This declares your time variable
FiveSecondsLater = clock() + 5 * CLOCKS_PER_SEC;

Now at this point, you'll have a variable "FiveSecondsLater" that equals the value that the system clock will be in five seconds. How does this work?

clock() will return a value something like 39492. This number goes up at a certain rate.
CLOCKS_PER_SEC is a constant. It is exactly how much clock() increases in a second.
So if you want to set a time for 5 seconds in the future, you multiply it by however many seconds you want to do.

So if you later want to do something after five seconds you'd use an if statement combined with the previous code.

if (clock() > FiveSecondsLater)
{
      // Do whatever it is you want to do.
}

Another function you could do here would be a generic "wait" function.

void wait (float seconds)
{
      clock_t EndTime = seconds * CLOCKS_PER_SEC;
      while (EndTime > clock())
      {
            // this will loop until clock() equals EndTime
      }

}

Well all C-based compilers have to add in the headers with '.h' following them. Later after the formation of c++ the programmers have ruled out the usage of '.h' and added c in the begininning

So
#include <ctime>
or
#include <time.h>
Refers to a header file with same content in it.

time.h is available so that even old c programs can be compiled using a c++ compiler.

>While I'm no expert at C++ I think I can help you with your dilema.
I don't think you can, sorry.

>I use the library time.h so you'd want to add
time.h is deprecated in standard C++. New code should be written using the ctime header instead.

>this is all assuming that you have using namespace std; in
>there as well, although I'm not sure that time.h requires it.
time.h does not place any names in the std namespace, but ctime does.

>Anyway the main two things you want to use are clock() and clock_t
Did you even bother reading the thread before replying? We've already established that the OP wants to work with date/time, not clock ticks.

>clock_t FiveSecondsLater; // This declares your time variable
>FiveSecondsLater = clock() + 5 * CLOCKS_PER_SEC;
That's nice. Now explain how that can be used to given the current wall clock time for different timezones.

>Another function you could do here would be a generic "wait" function.
Which is an extremely bad idea. A busy wait is both inefficient and anti-social.

now my program is giving me
project.cpp:564: error: syntax error at end of input

i have no idea what it means and my program is due friday!
my head is spinning here is my code.

#include<iostream>
#include<string>
#include<fstream>
#include <ctime>

using namespace std;

string area;
int zone;
ifstream indata;
string neg12[4];
string neg11[13];
string neg10[118];
string neg9[10];
string neg8[19];
string neg7[29];
string neg6[57];
string neg5[61];
string neg4[71];
string neg3[25];
string neg2[8];
string neg1[10];
string gm[52];
string pos1[98];
string pos2[71];
string pos3[40];
string pos4[23];
string pos5[25];
string pos6[20];
string pos7[25];
string pos8[31];
string pos9[27];
string pos10[24];
string pos11[16];
string pos12[32];
int a=1;

int main()
{
  time_t now=time(0);
  tm *local=localtime(&now);

 indata.open("negtwelve");
 indata>>neg12[a];

 while(indata)
 {
  indata>>neg12[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negeleven");
 indata>>neg11[a];

 while(indata)
 {
  indata>>neg11[a];
  a++;
 } 
a=1;

 indata.close();
 indata.open("negten");
 indata>>neg10[a];

 while(indata)
 {
  indata>>neg10[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negnine");
 indata>>neg9[a];

 while(indata)  
 {
  indata>>neg9[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negeight");
 indata>>neg8[a];

 while(indata)  
 {
  indata>>neg8[a];
  a++;
 }
 a=1;   

 indata.close();
 indata.open("negseven");
 indata>>neg7[a];
  
 while(indata)
 {
  indata>>neg7[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negsix");
 indata>>neg6[a];

 while(indata)  
 {
  indata>>neg6[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negfive"); a=1;

 indata.close();
 indata.open("negten");
 indata>>neg10[a];

 while(indata)
 {
  indata>>neg10[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negnine");
 indata>>neg9[a];

 while(indata)  
 {
  indata>>neg9[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negeight");
 indata>>neg8[a];

 while(indata)  
 {
  indata>>neg8[a];
  a++;
 }
 a=1;   

 indata.close();
 indata.open("negseven");
 indata>>neg7[a];
  
 while(indata)
 {
  indata>>neg7[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negsix");
 indata>>neg6[a];

 while(indata)  
 {
  indata>>neg6[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negfive"); a=1;

 indata.close();
 indata.open("negten");
 indata>>neg10[a];

 while(indata)
 {
  indata>>neg10[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negnine");
 indata>>neg9[a];

 while(indata)  
 {
  indata>>neg9[a];
  a++;
 }
 a=1;

 indata.close();
 indata.open("negeight");
 indata>>neg8[a];

 while(indata)  
 {
  indata>>neg8[a];
  a++;
 }
 a=1;   

 indata.close();
 indata.open("negseven");
 indata>>neg7[a];
  
 while(indata)
 {
  indata>>neg7[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negsix");
 indata>>neg6[a];

 while(indata)  
 {
  indata>>neg6[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negfive");
 indata>>neg5[a];

while(indata)   
 {
  indata>>neg5[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negfour");
 indata>>neg4[a];

 while(indata)  
 {
  indata>>neg4[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negthree");
 indata>>neg3[a];

 while(indata)  
 {
  indata>>neg3[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negtwo");
 indata>>neg2[a];
 
 while(indata)  
 {
  indata>>neg2[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("negone");
 indata>>neg1[a];
  
 while(indata)  
 {
  indata>>neg1[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("zero");
 indata>>gm[a];
  
 while(indata)  
 {
  indata>>gm[a]; 
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("one");
 indata>>pos1[a];
  
 while(indata)  
 {
  indata>>pos1[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("two");
 indata>>pos2[a];
  
 while(indata)  
 {
  indata>>pos2[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("three");
 indata>>pos3[a];
  
 while(indata)  
 {
  indata>>pos3[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("four");
 indata>>pos4[a];
  
 while(indata)  
 {
  indata>>pos4[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("five");
 indata>>pos5[a];
  
 while(indata)  
 {
  indata>>pos5[a];
  a++;
 }
 a=1;
  
 indata.close();
  
indata.close();
 indata.open("six");
 indata>>pos6[a];
 
 while(indata)   
 {
  indata>>pos6[a];
  a++;
 }
 a=1; 
  
 indata.close();
 indata.open("seven");
 indata>>pos7[a];
 
 while(indata)   
 {
  indata>>pos7[a];
  a++;
 }
 a=1; 
  
 indata.close();
 indata.open("eight");
 indata>>pos8[a];
 
 while(indata)   
 {
  indata>>pos8[a];
  a++;
 }
 a=1; 
  
 indata.close();
 indata.open("nine");
 indata>>pos9[a];
 
 while(indata)   
 {
  indata>>pos9[a];
  a++;
 }
 a=1; 
  
 indata.close();
 indata.open("ten");
 indata>>pos10[a];
 
 while(indata)   
 {
  indata>>pos10[a];
  a++;
 }
 a=1; 
  
 indata.close();
 indata.open("eleven");
 indata>>pos11[a];
  
 while(indata) 
 {
  indata>>pos11[a];
  a++;
 }
 a=1;
  
 indata.close();
 indata.open("twelve");
 indata>>pos12[a];
  
 while(indata)  
 {
  indata>>pos12[a];
  a++;
 }
 a=1;
  
 bool yes=true;
 while(yes==true)
 {    
  
   cout<<"Enter a country or country capitol, also you can enter a state capitl or major city. example: UNITED-STATES"<<endl;
   cin>>area;
   for(a=0; a<=3; a++)
   {
    if(area==neg12[a])
    {
     zone=120;
    local->tm_hour -=6;
    }
   }  
   for(a=0; a<=12; a++)
    {
    if(area==neg11[a])
     {
      zone=110;
      local->tm_hour -=5;
     }
    }
   for(a=0; a<=117; a++)
    {
    if(area==neg10[a])
     {
      zone=100; 
      local->tm_hour -=4;
     }
    }
   for(a=0; a<=9; a++)
    {
    if(area==neg9[a])
     {
      zone=90;
      local->tm_hour -=3;
     }
    }
   for(a=0; a<=18; a++)
    {
    if(area==neg8[a])
     {
      zone=80;
      local->tm_hour -=2;
     {
    }
   for(a=0; a<=28; a++)
    {
    if(area==neg7[a])
     {
      zone=70;
      local->tm_hour -=1;
     }
    }
   for(a=0; a<=56; a++)
    { 
    if(area==neg6[a])
      zone=60;
    }
   for(a=0; a<=60; a++)
   {
    if(area==neg5[a])
     {
      zone=50;
      local->tm_hour +=1;
     }
    }
   for(a=0; a<=70; a++)
    {
    if(area==neg4[a])
     {
      zone=40;
      local->tm_hour +=2;
     }
    }
   for(a=0; a<=24; a++)
    { 
    if(area==neg3[a])
     {
      zone=30;
      local->tm_hour +=3;
     }
    }
   for(a=0; a<=7; a++)
    { 
    if(area==neg2[a])
     {
      zone=20;
      local->tm_hour +=4;
     }
    }
   for(a=0; a<=9; a++)
    { 
    if(area==neg1[a])
     {
      zone=102;
      local->tm_hour +=5;
     }
    }
   for(a=0; a<=51; a++)
    { 
    if(area==gm[a])
     {
      zone=0;
      local->tm_hour +=6;
     }
    }
   for(a=0; a<=97; a++)
    { 
    if(area==pos1[a])
     {
      zone=1;
     local->tm_hour +=7;
     }
    } 
   for(a=0; a<=70; a++)
    {
    if(area==pos2[a])
     {
      zone=2;
      local->tm_hour +=8;
     }
    }
   for(a=0; a<=39; a++)  
    { 
    if(area==pos3[a])
     {
      zone=3;
      local->tm_hour +=9;
     }
    }
   for(a=0; a<=22; a++)  
    { 
    if(area==pos4[a])
     {
      zone=4;
      local->tm_hour +=10;
     }
    }
   for(a=0; a<=24; a++)  
    { 
    if(area==pos5[a])
     {
      zone=5;
      local->tm_hour +=11;
     }
    }
   for(a=0; a<=19; a++)  
    { 
    if(area==pos6[a])
     {
      zone=6;
      local->tm_hour +=12;
     }
    }
   for(a=0; a<=24; a++)  

    { 
    if(area==pos7[a])
     {
      zone=7;
      local->tm_hour +=13;
     }
    }
   for(a=0; a<=30; a++)  
    { 
    if(area==pos8[a])
     {
      zone=8;
      local->tm_hour +=14;
     }
    }
   for(a=0; a<=26; a++) 
    { 
    if(area==pos9[a])
     {
      zone=9;
      local->tm_hour +=15;
     }
    }
   for(a=0; a<=23; a++)  
    { 
    if(area==pos10[a])
     {
      zone=10;
      local->tm_hour +=16;
     }
    }
   for(a=0; a<=15; a++)  
    { 
    if(area==pos11[a])
     {
      zone=11;
      local->tm_hour +=17;
     }
    }
   for(a=0; a<=31; a++)   
    { 
    if(area==pos12[a])
     {
      zone=12;
      local->tm_hour +=18;
     }
    }
      
   time_t before=mktime(local);
     
   switch(zone)
    { 
     case 120:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 110:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 100:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 90:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 80:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 70:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 60: cout<<"the current time in "<<area<<" is";cout<< ctime ( &now );break;
     case 50:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 40:cout<<"the current time in "<<area<<" is";cout<< ctime( &before );break;
     case 30:cout<<"the current time in "<<area<<" is";cout<< ctime( &before );break;
     case 20:cout<<"the current time in "<<area<<" is";cout<< ctime( &before );break;
     case 102:cout<<"the current time in "<<area<<" is";cout<< ctime( &before );break;
     case 0:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 1:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 2:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 3:cout<<"the current time in "<<area<<" is";cout<< ctime ( &before );break;
     case 4:cout<<"the current time in "<<area<<" is";cout<<" ctime ( &before )";break;
     case 5:cout<<"the current time in "<<area<<" is";cout<< "ctime ( &before )";break;
     case 6:cout<<"the current time in "<<area<<" is";cout<<" ctime ( &before )";break;
     case 7:cout<<"the current time in "<<area<<" is";cout<< "ctime ( &before )";break;
     case 8:cout<<"the current time in "<<area<<" is";cout<<" ctime ( &before )";break;
     case 9:cout<<"the current time in "<<area<<" is";cout<<" ctime ( &before )";break;
     case 10:cout<<"the current time in "<<area<<" is";cout<<" ctime ( &before )";break;
     case 11:cout<<"the current time in "<<area<<" is";cout<< "ctime ( &before )";break;
     case 12:cout<<"the current time in "<<area<<" is";cout<<" ctime ( &before )";break;
     default: cout<<"we are sorry but "<<area<<" is not in our database. Please choose a different place. Thank you.";break;
    }
    cout<<endl;
    string again;
    cout<<"if you want to do this again type yes"<<endl;
    cin>>again;
    if(again=="yes")
      yes=true;
    else
     yes=false;
   }
 return 0;
}

It means you have a mismatched brace or double quote somewhere. The parser is reaching a logical end-of-file before the actual end-of-file and choking on the syntax error.

i have lined up all of my brackets and it still wont work any ideas?

I gave you my ideas already. I'm not going through that mess of code to tell you exactly what to fix. You're just going to have to remove valid chunks until you find the problem area.

i found it at the arry of neg8

Delete it and start again, after reading this.
http://cboard.cprogramming.com/showthread.php?t=88495

Write code a few lines at a time, then press compile to make sure you're still on track. Writing several hundred lines and dumping the whole sorry mess on a message board for someone else to fix isn't going to work.

When you get to a for loop, you can write this, then compile it.

for ( ; ; ) {
}

While loops and if statements would be

do {
} while ( 0 );
if ( 0 ) {
}

Or perhaps this, which allows you to keep the code you have, and comment out 99% of it in one easy step. You can then re-insert code a few lines at a time, as advised above.

int main ( ) {
#if 0
  cout << "A line";
  cout >> "another line";
  // and so on
#endif
}

NOW PRESS COMPILE.

Which on the first iteration becomes

int main ( ) {
  cout << "A line";
#if 0
  cout >> "another line";
  // and so on
#endif
}

NOW PRESS COMPILE.

And on the second iteration

int main ( ) {
  cout << "A line";
  cout >> "another line";
#if 0
  // and so on
#endif
}

NOW PRESS COMPILE.
At this point, you should have just one or two errors to deal with, and you know it has something to do with the last line you added to the code. Knowing that much should help you figure it out for yourself.


The point is, you should never write code beyond your ability to deal with the compiler spitting it back at you with a bunch of error messages.

i have already fixed it and took it to compitition i got a blue ribon which is first and i put all you guys in the bibliography

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.