473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

program output problem

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;
}
Feb 28 '07 #1
6 1239
Gary Wessle wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 28 '07 #2
"Victor Bazarov" <v.********@comAcast.netwrites:
Gary Wessle wrote:
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'.
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>&);
Feb 28 '07 #3
On Feb 28, 11:53 am, Gary Wessle <phd...@yahoo.comwrote:
I tried
getline( std::cin, a );
instead of
std::cin >a;

but I am doing something wrong.
std::getline(std::cin, a);

Best regards,

Tom
Feb 28 '07 #4
"Thomas Tutone" <Th***********@yahoo.comwrites:
On Feb 28, 11:53 am, Gary Wessle <phd...@yahoo.comwrote:
I tried
getline( std::cin, a );
instead of
std::cin >a;

but I am doing something wrong.

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;
}
Feb 28 '07 #5
Gary Wessle wrote:
"Thomas Tutone" <Th***********@yahoo.comwrites:

>>On Feb 28, 11:53 am, Gary Wessle <phd...@yahoo.comwrote:

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

but I am doing something wrong.

std::getline(std::cin, a);


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.

--
Ian Collins.
Feb 28 '07 #6
Ian Collins wrote:
Gary Wessle wrote:
>"Thomas Tutone" <Th***********@yahoo.comwrites:

>>On Feb 28, 11:53 am, Gary Wessle <phd...@yahoo.comwrote:
I tried
getline( std::cin, a );
instead of
std::cin >a;

but I am doing something wrong.

std::getline(std::cin, a);


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.
In order to overcome this, the 'getline' should be preceded by
'cin.ignore', probably. Or just read out a single character...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rafal Lagowski | last post by:
Hi all This is my small problem ... I have a program which make a long long statistic (about 2 minutes) It enough to timeout in my browser (or squid proxy of client) I want first make a...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
11
by: Ground21 | last post by:
Hello. I'm new // sorry for my english :) I have to write simple program i c++. But I don't know how to code src - program should know it's name (unit1.exe so the name is "unit1" or "unit1.exe"...
4
by: canteyn | last post by:
Ok here is my problem. I am coding a program that uses 3 different functions, and the end result is that the program reads in (x,y) points from a file, and then outputs those points to another file,...
3
by: partialprogressive | last post by:
I have a c++ program that runs with interactive text input and output with stdin and stdout. I want to change it to run from a server, with the interaction via web browser. It is not practical to...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.