 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vadivel Guest
|
Posted: Sat Feb 04, 2006 8:00 pm Post subject: Need Help |
|
|
Dear Friends,
I need your suggections to achive my task. My task is to find the add
the given time with then current system time.
I had done the following program, but I get some garbage value as my
result.
#include<iostream.h>
#include<conio.h>
#include<time.h>
void main(void)
{
time_t t1;
struct tm *time1;
int hh,mm,ss;
t1 = time(NULL);
*time1 = localtime(t1);
cout <<"current Time is : "<<asctime(time1);
cout<<"Enter the New Time : ";
cin>>hh>>mm>>ss;
time1->tm_sec += ss;
time1->tm_min += mm;
time1->tm_hour += hh;
cout<<"The Added Time is : "<<asctime(time1);
}
Can Any one help me what is the correction to be done to get the
correct result and also can anyone tell me is there any built-in
function to find the difference between given time and system time.
Thanks you all.
By,
Vadivel P.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sun Feb 05, 2006 2:00 pm Post subject: Re: Need Help |
|
|
"Vadivel" <vadivelpvs (AT) gmail (DOT) com> writes:
| Quote: | #include<iostream.h
#include<conio.h
|
These are not Standard C++ headers.
Consider #including <iostream>, <istream> and <ostream> instead of
<iostream.h>. I don't see anything in your code that would be declared
in <conio.h> as present on most Windows C++ implementations.
And while we are at it, consider #including <ctime> instead.
Changing these #includes according to my suggestions means that all
entities from the Standard C++ Library you are using in your program,
except NULL, belong to the namespace std. Qualify them with the prefix
std:: .
The main() function's return type has to be int.
| Quote: | {
time_t t1;
struct tm *time1;
int hh,mm,ss;
|
Consider not defining variables at the beginning of a block, but right
where you know how to initialize them. This will prevent you from
forgetting to initialize a variable before reading it ...
| Quote: |
t1 = time(NULL);
*time1 = localtime(t1);
|
.... as here. The sub-expression *time1 dereferences a pointer that has
not been initialized yet.
Note that localtime() returns a pointer to tm. But the expression
*time1 is of type tm (not *pointer to* tm). You probably mean to write
time1 = localtime(t1);
| Quote: | cout <<"current Time is : "<<asctime(time1);
|
localtime() can return NULL. Check for that before passing time1 to
asctime().
asctime() can return NULL, too. Check for that before passing the
return value to operator<<.
| Quote: | cout<<"Enter the New Time : ";
cin>>hh>>mm>>ss;
|
These input operations can fail. Check for success before using the
values (apparently) read ...
| Quote: | time1->tm_sec += ss;
time1->tm_min += mm;
time1->tm_hour += hh;
|
.... or all these operations have undefined behavior. E.g.:
if (std::cin >> hh >> mm >> ss)
{
time1->tm_sec += ss;
time1->tm_min += mm;
time1->tm_hour += hh;
}
else
{
// error handling
}
Since you seem to be new to C++, reading a good textbook would be a
good idea. Have a look at the reviews at http://www.accu.org/ to find
one.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Vadivel Guest
|
Posted: Mon Feb 06, 2006 2:00 pm Post subject: Re: Need Help |
|
|
Dear Friends,
First I'm very sorry to say I had made a Mistake in the program posted
here.
I like to intimate correct my program which is posted here for the
clarification.
the above program is properly running now error is indicated here if it
is executed as it is.
But When I omit the any of the following statement it produce an
garbabe value, in the section for which I haven't done the calculation.
ie.
the statements
time->tm_hour += hh;
time->tm_min += mm;
time->tm_sec += ss;
is functioning correctly, but when I use the following calculation
time->tm_hour += hh;
time->tm_min += mm;
it shows the garbage value for time->tm_sec.
So, Please correct the code with these and give my an explaination and
how it can be corrected.
Once again I ask you all to please apology me to made this major
mistakes. I will avoid such mistakes in my future.
Thanking you.
By
Vadivel P.
India.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Vadivel Guest
|
Posted: Mon Feb 06, 2006 3:00 pm Post subject: Re: Need Help |
|
|
Respected Thomas,
Thank you for your clarifications.
As u said,
| Quote: | you seem to be new to C++
|
I'm new to the C++ programming. I'm following one Textbook of Mr.
Kanetkar & Mr.Ravichandran, those who are one of the famous writers in
India, Especially followed in our State.
According their book the usage of Header files is followed my me.
In my case regarding the program given here, is taken from the c++ help
environment and modified according to my need.
As you said,
| Quote: | t1 = time(NULL);
*time1 = localtime(t1);
... as here. The sub-expression *time1 dereferences a pointer that has not been initialized yet.
Note that localtime() returns a pointer to tm. But the expression
*time1 is of type tm (not *pointer to* tm). You probably mean to write
time1 = localtime(t1);
cout <<"current Time is : "<<asctime(time1);
localtime() can return NULL. Check for that before passing time1 to
asctime().
asctime() can return NULL, too. Check for that before passing the
return value to operator<<.
|
I had checked the above statment's in my compiller and it is properly
working (ie, displaying the current system time, which doesn't desplay
NULL) and these statements were copied from the EXAMPLE provided in the
C++ Help window.
| Quote: | cout<<"Enter the New Time : ";
cin>>hh>>mm>>ss;
These input operations can fail. Check for success before using the
values (apparently) read ...
|
| Quote: | time1->tm_sec += ss;
time1->tm_min += mm;
time1->tm_hour += hh;
... or all these operations have undefined behavior.
|
I had Checked these statements too, which is working properly. but I
got some garbage value when I'm doing the calculation by omiting any of
the following statement.
So, Please check the coding given by me here with your complier and
give the solution for this.
once again thanking you for your suggestion.
By,
Vadivel P.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Feb 06, 2006 11:01 pm Post subject: Re: Need Help |
|
|
"Vadivel" <vadivelpvs (AT) gmail (DOT) com> writes:
| Quote: | As u said,
you seem to be new to C++
I'm new to the C++ programming. I'm following one Textbook of Mr.
Kanetkar & Mr.Ravichandran, those who are one of the famous writers
in India, Especially followed in our State.
According their book the usage of Header files is followed my me.
|
In that case, their book may be outdated.
| Quote: | localtime() can return NULL. Check for that before passing time1 to
asctime().
asctime() can return NULL, too. Check for that before passing the
return value to operator<<.
I had checked the above statment's in my compiller and it is
properly working (ie, displaying the current system time, which
doesn't desplay NULL) and these statements were copied from the
EXAMPLE provided in the C++ Help window.
|
Doesn't matter. Show the program to your customer or your boss, and
one of the functions will return NULL.
| Quote: | So, Please check the coding given by me here with your complier and
give the solution for this.
|
No. You are asking question, so you do the work :-)
Add the checks I suggested, and if the problem persists, post your
program here again.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Tue Feb 07, 2006 1:00 pm Post subject: Re: Need Help |
|
|
Thomas Maeder wrote:
| Quote: | "Vadivel" <vadivelpvs (AT) gmail (DOT) com> writes:
As u said,
you seem to be new to C++
I'm new to the C++ programming. I'm following one Textbook
of Mr. Kanetkar & Mr.Ravichandran, those who are one of the
famous writers in India, Especially followed in our State.
According their book the usage of Header files is followed my me.
In that case, their book may be outdated.
|
Not just outdated, since it included <conio.h> and had a void
return from main -- neither of which has ever been part of C++.
In practice, there are three levels of "problems" with the
headers
-- As far as I know, the use of <conio.h> is just plain wrong
in any program today. I think it's just a compatibility
header under Windows, to simplify the migration from MS-DOS.
At any rate, it's not C++, and never was.
-- The use of <iostream.h> is outdated. People learning C++
should avoid it, even though it is sometimes (albeit less
and less frequently) necessary in industry for compatibility
with legacy code.
-- The use of <type.h> is more debatable. First, of course,
unlike <iostream.h>, its contents are defined by the current
standard. And secondly, to date I've yet to see a standards
conformant implementation of <ctime>. So even in my most
up-to-date work, I continue to use the .h headers for the C
headers.
I'd tend to avoid books with the first problem, period. They're
just plain wrong (unless the book is an introduction to MS-DOS
programming, of course).
I'd look for a more up-to-date version of a book with the second
problem, but if there wasn't one, I wouldn't worry about it too
much. Although I'm less sure in the case of a beginner. There
are perfectly good introductory texts which do use the standard
headers, and a book which doesn't use <iostream> et al. is
equally likely not to use std::vector. If the goal is
explaining some other aspect of comuter science, and C++ is just
the medium, fine, but if the goal is an up to date introduction
to C++...
Finally, as for the third, it wouldn't worry me the least in a
book.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|