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;
}
Related
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;
}
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!
From the console i am asking for a hexadecimal string to convert to a pointer to reference an item in memory.
#include <iostream>
#include <sstream>
#include <Windows.h>
int char_to_pointer(std::string input);
int main() {
int sample = 100; // lets say this address is 0xc1f1
std::string input_;
std::cout << "addr:" << &sample << std::endl;
std::cout << "what is the memory address?:" << std::endl;
std::cin >> input_;
unsigned int inp = char_to_pointer(input_);
std::cout << "imp: " << inp << std::endl;
Sleep(10000);
return 0;
}
int char_to_pointer(std::string input) {
return std::stoul(input, nullptr, 16);
}
My problem is that char_to_pointer only converts the hex string into a decimal.
this is what i want:
input: "0xc1f1"
output: 100
I found the solution:
#include <iostream>
#include <sstream>
#include <Windows.h>
#include <string>
int *char_to_pointer(std::string input);
int main() {
int sample = 100; // lets say this address is 0xc1f1
std::string input_;
std::cout << "addr:" << &sample << std::endl;
std::cout << "what is the memory address?:" << std::endl;
std::cin >> input_;
int *inp = char_to_pointer(input_);
std::cout << "imp: " << inp << std::endl;
std::cout << "imp*: " << *inp << std::endl;//This was my solution
std::cout << "imp&: " << &inp << std::endl;
Sleep(10000);
return 0;
}
int *char_to_pointer(std::string input) {
return (int *)std::stoul(input, nullptr, 16);
}
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;
}
Is there a way to round to the nearest number in the Boost library? I mean any number, 2's, 5's, 17's and so on and so forth.
Or is there another way to do it?
You can use lround available in C99.
#include <cmath>
#include <iostream>
int main() {
cout << lround(1.4) << "\n";
cout << lround(1.5) << "\n";
cout << lround(1.6) << "\n";
}
(outputs 1, 2, 2).
Check your compiler documentation if and/or how you need to enable C99 support.
Boost Rounding Functions
For example:
#include <boost/math/special_functions/round.hpp>
#include <iostream>
#include <ostream>
using namespace std;
int main()
{
using boost::math::lround;
cout << lround(0.0) << endl;
cout << lround(0.4) << endl;
cout << lround(0.5) << endl;
cout << lround(0.6) << endl;
cout << lround(-0.4) << endl;
cout << lround(-0.5) << endl;
cout << lround(-0.6) << endl;
}
Output is:
0
0
1
1
0
-1
-1
int nearest = 5;
int result = (input+nearest/2)/nearest*nearest;
You actually don't need Boost at all, just the C library included in the C++ library. Specifically, you need to include the cmath header:
Round up a number: ceil(): http://www.cplusplus.com/reference/clibrary/cmath/ceil/
Round down a number: floor(): http://www.cplusplus.com/reference/clibrary/cmath/floor/
You can write your own round function then:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
double roundFloat(double x)
{
double base = floor( x );
if ( x > ( base + 0.5 ) )
return ceil( x );
else return base;
}
int main()
{
std::string strInput;
double input;
printf( "Type a number: " );
std::getline( std::cin, strInput );
input = std::atof( strInput.c_str() );
printf( "\nRounded value is: %7.2f\n", roundFloat( input ) );
return EXIT_SUCCESS;
}