program output problem

G

Gary Wessle

Hi

the following code puts out
****************************************************************
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 10:50:00
1172581200
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 21:00:00
1172581200
****************************************************************

the same epoch for 2 different time_points, i.e
2007-2-28 21:00:00
2007-2-28 10:50:00
bot giving out
1172581200

here is the code, could some one please take a look and tell me whats
gone wrong.

thank you

****************************************************************
#include <iostream>
#include <string>
#include <ctime>


/* Returns local time epoch from a given time format
"2007-01-28 21:15:00" */
time_t asci2epoch(std::string s)
{
struct tm t_tm = { 0 };
// the time string
const char *t_str = s.c_str();
// get the values from the time string
sscanf(t_str, "%d-%d-%d %d:%d:%d", &(t_tm.tm_year), &(t_tm.tm_mon), &(t_tm.tm_mday), &(t_tm.tm_hour), &(t_tm.tm_min), &(t_tm.tm_sec));
// normalize ...
t_tm.tm_year -= 1900;
t_tm.tm_mon -= 1;
// the result :
time_t t = mktime(&t_tm);
t -= (1*60*60); // DST
return t;
}

int main()
{

std::cout << "Type 1 for epoch-to-text or 2 for text-to-epoch." << std::endl;
short opt;
std::cin >> opt;
if( opt == 1 )
{
time_t x;
std::cin >> x;
tm* p_tick = localtime( &x );
std::cout << "tick: " << asctime( p_tick ) << std::endl;
}
else if( opt == 2 )
{
std::string a;
std::cin >> a; // std::string a = "2007-2-5 8:45:00";
std::cout << asci2epoch(a) << std::endl;
}
else
std::cout << "wrong option." << std::endl;
}
 
V

Victor Bazarov

Gary said:
Hi

the following code puts out
****************************************************************
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 10:50:00
1172581200
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 21:00:00
1172581200
****************************************************************

the same epoch for 2 different time_points, i.e
2007-2-28 21:00:00
2007-2-28 10:50:00
bot giving out
1172581200

here is the code, could some one please take a look and tell me whats
gone wrong.

Inputting a string is wrong.
thank you

****************************************************************
#include <iostream>
#include <string>
#include <ctime>


/* Returns local time epoch from a given time format
"2007-01-28 21:15:00" */
time_t asci2epoch(std::string s)
{
struct tm t_tm = { 0 };
// the time string
const char *t_str = s.c_str();
// get the values from the time string
sscanf(t_str, "%d-%d-%d %d:%d:%d", &(t_tm.tm_year), &(t_tm.tm_mon),
&(t_tm.tm_mday), &(t_tm.tm_hour), &(t_tm.tm_min), &(t_tm.tm_sec));
// normalize ... t_tm.tm_year -= 1900;
t_tm.tm_mon -= 1;
// the result :
time_t t = mktime(&t_tm);
t -= (1*60*60); // DST
return t;
}

int main()
{

std::cout << "Type 1 for epoch-to-text or 2 for text-to-epoch." <<
std::endl; short opt;
std::cin >> opt;
if( opt == 1 )
{
time_t x;
std::cin >> x;
tm* p_tick = localtime( &x );
std::cout << "tick: " << asctime( p_tick ) << std::endl;
}
else if( opt == 2 )
{
std::string a;
std::cin >> a; // std::string a = "2007-2-5 8:45:00";

Check what you get in your string when you input it. Then read
about 'getline'.
std::cout << asci2epoch(a) << std::endl;
}
else
std::cout << "wrong option." << std::endl;
}

V
 
G

Gary Wessle

Victor Bazarov said:
Inputting a string is wrong.


Check what you get in your string when you input it. Then read
about 'getline'.

I tried
getline( std::cin, a );
instead of
std::cin >> a;

but I am doing something wrong.
isn't
template<class Ch, class Tr, class A>
basic_istream<Ch, Tr>& getline(basic_istream<Ch, Tr>&,
basic_string<Ch, Tr, A>&);
 
G

Gary Wessle

Thomas Tutone said:
std::getline(std::cin, a);

the code does not wait for entry and it ends prematurely when
selecting the "2" option.

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


/* Returns local time epoch from a given time format
"2007-01-28 21:15:00" */
time_t asci2epoch(std::string s)
{
struct tm t_tm = { 0 };
// the time string
const char *t_str = s.c_str();
// get the values from the time string
sscanf(t_str, "%d-%d-%d %d:%d:%d", &(t_tm.tm_year), &(t_tm.tm_mon), &(t_tm.tm_mday), &(t_tm.tm_hour), &(t_tm.tm_min), &(t_tm.tm_sec));
// normalize ...
t_tm.tm_year -= 1900;
t_tm.tm_mon -= 1;
// the result :
time_t t = mktime(&t_tm);
t -= (1*60*60); // DST
return t;
}

int main()
{

std::cout << "Type 1 for epoch-to-text or 2 for text-to-epoch." << std::endl;
short opt;
std::cin >> opt;
if( opt == 1 )
{
time_t x;
std::cin >> x;
tm* p_tick = localtime( &x );
std::cout << "tick: " << asctime( p_tick ) << std::endl;
}
else if( opt == 2 )
{
std::string a;
std::getline( std::cin, a); // std::string a = "2007-2-5 8:45:00";
std::cout << asci2epoch(a) << std::endl;
}
else
std::cout << "wrong option." << std::endl;
}
 
I

Ian Collins

Gary said:
the code does not wait for entry and it ends prematurely when
selecting the "2" option.
It's picking up the newline character that wasn't read from cin when the
option is input.
 
V

Victor Bazarov

Ian said:
It's picking up the newline character that wasn't read from cin when
the option is input.

In order to overcome this, the 'getline' should be preceded by
'cin.ignore', probably. Or just read out a single character...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top