How can I get current date in c++? - c++

I did not find any solution which give the only date. I found the solution but all are complex and we have to parse the array and separate the date from time

Try to this
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}

If you run the following code, today then you will find the
current date in following format.
04/14/15
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
char c[9];
_strdate_s(c);
cout<<c<<endl;
return 0;
}

ideone code
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0);
tm *ltm = localtime(&now);
cout << "Year: "<< 1900 + ltm->tm_year << endl;
cout << "Month: "<< 1 + ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
}

Related

Creating new txt files with c++ program, naming with variables

I have created a program and I want to create with it files like aff1.txt, aff2.txt, etc. In these files, I want to have here a text created this way: It will open the file: text.txt and it will take each sentence, copy it 4700/sentence length times to each file. But it isn't working, when: cout << ss << endl;, it writes to cmd nothing, while there should be something, which was assigned before. What should I do?
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
int main()
{
ifstream vstup("text.txt"); // 4700,2700,2200,1700
string vety;
getline(vstup,vety);
vstup.close();
string ss="affn.txt";
char q[vety.length()];
for (int u=0;u<vety.length();u++)
{
q[u] = vety[u];
}
int l=0,m=0,n=0;
int v,i,e,o;
char vl[999999];
//cout << vety.length() << endl;
for (i=0;i<vety.length();i++)
{
//cout << "ss" << endl;
if (q[i]=='.')
{
// cout << "ss" << endl;
v=4700/i;
for (e=0;e<v;e++)
{
//cout << "ss" << endl;
for (o=0;o<i-l;o++)
{
// cout << "ss" << endl;
m=o+e*(i-l);
vl[m]=q[o+l];
}
}
l=l+i;
cout << vl << endl;
n++;
//ofstream aff("aff.txt");
//aff << vl << endl;
//aff.close();
ss[3]=n;
ofstream writer(ss.c_str());
//writer.open(ss.c_str());
writer << vl << endl;
writer.close();
cout << ss << endl;
ss.clear();
}
}
return 0;
}

why when using while the var is not updated and print after start var only?

I wanna make a code to sync clock now and clock targeted, but the code print the same clock as first start when using while.
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;
int main(){
time_t now = time(0);
tm *ltm = localtime(&now);
do{
if(ltm->tm_sec > 60){
cout << "ok : " << ltm->tm_sec << endl;
break;
} else {
cout << "ok : " << ltm->tm_sec << endl;
continue;
}
}while (ltm->tm_sec <= 60);
}
I expect it will print the new clock now but it print as same as when start the code time.
You need to update now & ltm variable with current time inside do-while loop.
You may change your code as below:
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(0);
tm *ltm;
do
{
now = time(0);
ltm = localtime(&now);
if(ltm->tm_sec > 60)
{
cout << "ok : " << ltm->tm_sec << endl;
break;
}
else
{
cout << "ok : " << ltm->tm_sec << endl;
continue;
}
}while (ltm->tm_sec <= 60);
return 0;
}
I hope it works!

C++ nested loop, increasing base by power

I need some help. I want to achieve a result as shown on my image result
first question... did my teacher make a mistake? 2^10 = 1024 and not 2048... ?! but nvm..
my only problem is the cout of numbers over 100.000 - please help
here's my code so far
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#include <sstream>
#include <ostream>
#include <stdio.h>
#include <conio.h>
#include <fstream>
using namespace std;
int main() {
int p; // exponent
float b; // base
cout << setw(4);
for (b=1; b<11; b++) {
cout << " | " << setw(12) << b;
}
cout << endl;
for (b=1; b<=10; b++) {
cout << b;
for (p=1; p<=10; p++) {
cout << " | " << setw(12) << pow(b, p);
}
cout << endl;
}
return 0;
}
Output of this is here
pls pls help,
best regards!
If the issue is output, then the easiest thing to do is cast the pow() return value to a long long or whatever your compiler's 64-bit int type is.
This will automatically use the long long overload for operator <<, which will effectively remove the scientific notation.
for (p=1; p<=10; p++) {
cout << " | " << static_cast<long long>(pow(b, p));
Live Example

Time of clock is not updating in c++

#include <ctime>
#include <iostream>
using namespace std;
int main() {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << now->tm_mday << '-'//day
<< (now->tm_mon +1 ) << '-'//month
<< (now->tm_year +1900 )//year
<<endl
<<now->tm_hour//hour
<<'-'<<now->tm_min//min
<<'-'<< now->tm_sec//sec
<< endl;
return 0;
}
This piece of code gives me the system date and time,the only problem i have is that the time is not updating.
For example:= the time is not moving forward and is stuck at a fixed time like 1.33.10
You need to clear the console and display date again every a given amount of time.
Consider following example (compile with -std=c++11):
#include <iostream> // IO
#include <ctime> // time
#include <thread> // threads
#include <chrono> // chrono
int main(){
while(true){
system("clear"); // clear console
time_t t = time(0); // get time now
struct tm* now = localtime(&t);
std::cout << now->tm_mday << '-' // day
<< (now->tm_mon + 1) << '-' // month
<< (now->tm_year + 1900) << std::endl // year
<< now->tm_hour << '-' // hour
<< now->tm_min << '-' // min
<< now->tm_sec << std::endl // sec
;
// sleep for 1000 ms (=1s)
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}
In given code, we infinitelly loop the code that:
Clears the console
Recout the date
Waits 1000ms (1s)

C++ converting string to time_t question

Im trying to convert a string such as "12 August 2011" into time_t or that seconds elapsed, or whatever so I can use it to compare a list of dates.
At the moment, I have tried the following but the output seems to equal false! Also, the seconds elapsed seem to keep changing?
Is this correct?
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <time.h>
#include <stdio.h>
using namespace std;
int main()
{
struct tm tmlol, tmloltwo;
time_t t, u;
t = mktime(&tmlol);
u = mktime(&tmloltwo);
//char test[] = "01/01/2008";string test = "01/01/2008";
strptime("10 February 2010", "%d %b %Y", &tmlol);
strptime("10 February 2010", "%d %b %Y", &tmloltwo);
t = mktime(&tmlol);
u = mktime(&tmloltwo);
cout << t << endl;
cout << u << endl;
if (u>t)
{
cout << "true" << endl;
}
else if (u==t)
{
cout << "same" << endl;
}
else
{
cout << "false" << endl;
}
cout << (u-t);
}
You should initialize the structs before use. Try this:
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <time.h>
#include <stdio.h>
using namespace std;
int main()
{
struct tm tmlol, tmloltwo;
time_t t, u;
// initialize declared structs
memset(&tmlol, 0, sizeof(struct tm));
memset(&tmloltwo, 0, sizeof(struct tm));
strptime("10 February 2010", "%d %b %Y", &tmlol);
strptime("10 February 2010", "%d %b %Y", &tmloltwo);
t = mktime(&tmlol);
u = mktime(&tmloltwo);
cout << t << endl;
cout << u << endl;
if (u>t)
{
cout << "true" << endl;
}
else if (u==t)
{
cout << "same" << endl;
}
else
{
cout << "false" << endl;
}
cout << (u-t) << endl;
return 0;
}