473,396 Members | 1,961 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,396 software developers and data experts.

Why this function doesent work correctly...??

Dear,

I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
Here s code compiled with gcc
it goes (if you name program as "transform"),
transform 1000, for 1000 seconds...

#include<stdio.h>

void transform(char*, int);

int main(int argc, char* argv[]){
int time;
char formated_t[12];
time=atoi(argv[1]);
transform(&formated_t, time);
printf("\n%s", formated_t);
}

void transform(char t[], int time){
int days, hours, minuts, seconds, i, j, k;
days=time/(24*3600);
i=time%(24*3600);
time=time/(24*3600)+i;
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
i=time%60;
time=time/60+i;
seconds= time % 60;
sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);
}
Thanks in advance, Robert !!
ro***********@si.t-com.hr

Nov 15 '05 #1
11 1827
"Bore Biko" <bo*******@yahoo.co.uk> wrote in message
news:dg**********@ss405.t-com.hr...
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
Assuming we have equal understanding of what is correct... :)
Here s code compiled with gcc
it goes (if you name program as "transform"),
transform 1000, for 1000 seconds...

#include<stdio.h>

void transform(char*, int);

int main(int argc, char* argv[]){
int time;
char formated_t[12];
time=atoi(argv[1]);
transform(&formated_t, time);
^^^^^^^^^^^ you must not use & here. Actually, if you invoke gcc with the
switch -Wall (which you should always put or you're in trouble), then gcc
will warn you. The idea is simple, pointer to array and pointer to array's
first element are both pointers (equal pointers) but of somewhat different
type. Don't ask me why -- it's by design of C...
printf("\n%s", formated_t);
}

void transform(char t[], int time){
^^^ here t is effectively a pointer to a char (or first element of an array,
but not the array itself)
int days, hours, minuts, seconds, i, j, k;
days=time/(24*3600);
ok
i=time%(24*3600);
ok
time=time/(24*3600)+i;


and what do you want to say by the above? Whay are you summing up? Cats and
cans? :)
Don't you think something's unnecessary here, either cats or cans?

I hope you'll fix this and any remaining probs yourself.

Alex
Nov 15 '05 #2
On Tue, 20 Sep 2005 23:22:43 +0200, in comp.lang.c , "Bore Biko"
<bo*******@yahoo.co.uk> wrote:
Dear,

I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
Please define what you mean by incorrect - crashs, produces nonsense,
is nearly right but sometimes gives off-by-one errors?

Anyway, turn up warning levels on your compiler and try again....
#include<stdio.h> void transform(char*, int);

int main(int argc, char* argv[]){
int time;
char formated_t[12];
time=atoi(argv[1]);
missing header for atoi()
transform(&formated_t, time);
transform takes a char*, you're passing it a char**. Remove the &
printf("\n%s", formated_t);
}

void transform(char t[], int time){
int days, hours, minuts, seconds, i, j, k;


j & k are not used.

The rest will work as you expect, more or less, for values less than
one day. Over that, you need to be a bit cleverer.

You may also want to read up on the formatting string for printf, so
you can discover how to get two digits.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #3


Bore Biko wrote On 09/20/05 17:22,:
Dear,
Sshh! Not in front of the children!
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
"It doesn't work correctly" is no more informative
than "It's wrong." You haven't described what's wrong,
why you think it's wrong, or how it differs from what
you expected. I'll try to guess, but if I'm attacking
the wrong problem you have only yourself to blame.
Here s code compiled with gcc
... but with the warnings muted, or else gcc
would have issued complaints. (In fact, it should
have complained anyhow; are you sure you've shown
us exactly the same code you've been compiling?)

For nearly all code I'd recommend using
"gcc -W -Wall". For highly portable code, go even
further to "gcc -W -Wall -ansi -pedantic" (or you
might replace "-ansi" with "-std=whatever").
it goes (if you name program as "transform"),
transform 1000, for 1000 seconds...

#include<stdio.h>
You forgot to #include <stdlib.h>.
void transform(char*, int);

int main(int argc, char* argv[]){
int time;
char formated_t[12];
time=atoi(argv[1]);
It might have been wise to make sure argv[1] is
actually present before trying to convert it ... A
quick look at the value of argc would tell the tale.

Also, atoi() is not a very good way to convert
strings to numbers. The problem is that it cannot be
relied upon to do anything sensible if the input is
something like "zaphod" instead of a numeric string.
It also can't be relied upon if the input is something
along the lines of "99999999999999999999999999999999".
Functions like strtol() are much safer.
transform(&formated_t, time);
Here's what the compiler should have complained
about: the function's first argument is supposed to
be a pointer to a `char', but you're passing a pointer
to an array of `char' instead. If you don't understand
the difference, see Question 6.12 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
printf("\n%s", formated_t);
A subtle bug that might not bother you at the
moment but that will (by Murphy's Law) bite you at
some other, more important moment: The last character
sent to a text output stream before it is closed
must be a '\n', or the final line might never appear
or might appear in some muddled way. Some systems will
let you get away with this; others will not.

You have (correctly) defined main() as returning
an `int' value, but you haven't actually returned one.
There's a special dispensation for this in the latest
version of the C Standard, but more compilers hew to
the older version than to the new one, so it's unwise
to rely on it as yet.
}

void transform(char t[], int time){
int days, hours, minuts, seconds, i, j, k;
days=time/(24*3600);
Here's another subtle trap, less common nowadays
than it used to be, but still occasionally stumbled
upon like a forgotten land mine. `24' and `3600' are
`int' constants, so their product will be computed as
an `int'. However, the range of `int' varies from one
compiler to the next, and its upper limit can be as low
as 32767. Since the product 86400 is well above this
limit, the computation could overflow. For perfect
safety, you should carry it out in `long' instead.
i=time%(24*3600);
All right: `i' is now the number of seconds left
over once you've taken out the days.
time=time/(24*3600)+i;
... but what's this? You're adding the number of
days -- not the number of seconds in those days, just
the days themselves -- to the number of left-over seconds.
You then pursue the same pattern in the rest of the code,
but the pattern itself is nonsensical. Seventy-one seconds
is one minute ten seconds, not one minute eleven. Work a
few of these conversions with pencil and paper, study the
steps you take, and reproduce those steps in code.
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
i=time%60;
time=time/60+i;
seconds= time % 60;
sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);
Two problems here. First, if the original number of
seconds is large enough you'll generate more characters than
will fit in the space allotted to `t'. 8726399 seconds would
be 100:23:59:59, which (with the terminating '\0') would try
to put thirteen characters in a twelve-character sack, with
unpredictable consequences.

Second, the plain "%d" conversion will not always generate
the two digits you desire. You'll get things like 1:13:0:59:1
instead of 01:13:00:59:01. To force two positions, use "%2d".
To fill with leading zeroes instead of leading blanks, use
"%02d".
}


--
Er*********@sun.com

Nov 15 '05 #4

"Eric Sosman" <er*********@sun.com> wrote in message
news:dg**********@news1brm.Central.Sun.COM...


Bore Biko wrote On 09/20/05 17:22,:
Dear,


Sshh! Not in front of the children!

I don't know what to put a young lady who is English teacher
told me that is usualy put: "Dear"
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...

here was a problem:
void transform(char* t, int time){
int days, hours, minuts, seconds, i, timi;
char dd[5],hh[3],mi[3],sec[3];
timi=time;
days=time/(24*3600);
i=time%(24*3600);
time=time/(24*3600)+i;
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
time=time/60;
time=time%60;
seconds=timi-days*3600*24-hours*3600-minuts*60;

now its all OK..!!!
Nov 15 '05 #5
Robert Bralic wrote:
I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...

here was a problem:
void transform(char* t, int time){
int days, hours, minuts, seconds, i, timi;
char dd[5],hh[3],mi[3],sec[3];
timi=time;
days=time/(24*3600);
i=time%(24*3600);
time=time/(24*3600)+i;

^^^^^^^^^^^^^^^^^^^^^^
This makes no sense
hours=(time/3600);
i=time%3600;
time=time/3600+i; ^^^^^^^^^^^^^^^^^
Neither does this
minuts=time/60;
time=time/60;
time=time%60;
seconds=timi-days*3600*24-hours*3600-minuts*60; now its all OK..!!!


No it isn't. Your computations are still wrong. You may not have
noticed, but I am pretty sure that the output will be incorrect for
some input values.

Take a paper and a pencil and make the computations manually. Then
describe what you have just done in C. It's not that difficult.

Nov 15 '05 #6
now its all OK..!!!


No it isn't. Your computations are still wrong. You may not have
noticed, but I am pretty sure that the output will be incorrect for
some input values.

Take a paper and a pencil and make the computations manually. Then
describe what you have just done in C. It's not that difficult.

Here is a simple program for generating random numbers,
with implemented function...
Sory becouse of all this smal strings and ifs I forgoted
to use "%02d" format...
//---program is compiled with gcc and works correct-------------------------
//--just compile with "gcc prog.c -o prog.exe and then do prog
number--------
#include<stdio.h>
#include<malloc.h>
#include<time.h>

void transform(char*, int);
struct lst{
long int value;
struct lst* next;
};

int main(int argc, char* argv[]){
long int i,j,k, time_begin, time_end, razlika_vremena;
char trans_value[20];
struct lst *prvi, *tekuci_prvi, *tekuci_drugi, *zadnji;
prvi=(struct lst*)malloc(sizeof(struct lst));
prvi->next=(struct lst*)malloc(sizeof(struct lst));
prvi->value=1;
prvi->next->value=2;
tekuci_prvi=prvi->next;
zadnji=prvi->next;
zadnji->next=(struct lst*)NULL;
k=atoi(argv[1]);
tekuci_drugi=prvi;
time(&time_begin);
for(i=1;i<k;i++){
exit:
tekuci_drugi=prvi;
while(tekuci_drugi->next){
if((i%(tekuci_drugi->value)==0) && tekuci_drugi->value!=1){
if(i==1){
goto exit;
}
i++;
goto exit;
}
tekuci_drugi=tekuci_drugi->next;
}
tekuci_prvi->next=(struct lst*)malloc(sizeof(struct lst));
tekuci_prvi=tekuci_prvi->next;
tekuci_prvi->value=i;
tekuci_prvi->next=(struct lst*)NULL;
/*printf("%ld\n",i);*/
}
tekuci_prvi=prvi;
while(tekuci_prvi->next){
printf("%d\n", tekuci_prvi->value);
tekuci_prvi=tekuci_prvi->next;
}
time(&time_end);
razlika_vremena=time_end-time_begin;
transform(&trans_value, razlika_vremena);
printf("\nGeneriranje i stampanje do %d broja je trajalo:%s", k,
trans_value);
exit(0);
}


void transform(char* t, int time){
int days, hours, minuts, seconds, i, timi;
char dd[5],hh[3],mi[3],sec[3];
timi=time;
days=time/(24*3600);
i=time%(24*3600);
time=time/(24*3600)+i;
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
time=time/60;
time=time%60;
seconds=timi-days*3600*24-hours*3600-minuts*60;
if(days<10){
sprintf(dd,"0%d",days);
}else{
sprintf(dd,"%d",days);
}
if(hours<10){
sprintf(hh,"0%d",hours);
}else{
sprintf(hh,"%d",hours);
}
if(minuts<10){
sprintf(mi,"0%d",minuts);
}else{
sprintf(mi,"%d",minuts);
}
if(seconds<10){
sprintf(sec,"0%d",seconds);
}else{
sprintf(sec,"%d",seconds);
}
sprintf(t,"%s:%s:%s:%s",dd, hh, mi, sec);
}


Nov 15 '05 #7
Robert Bralic wrote:
now its all OK..!!!


No it isn't. Your computations are still wrong. You may not have
noticed, but I am pretty sure that the output will be incorrect for
some input values.

Take a paper and a pencil and make the computations manually. Then
describe what you have just done in C. It's not that difficult.

Here is a simple program for generating random numbers,
with implemented function...
Sory becouse of all this smal strings and ifs I forgoted
to use "%02d" format...


<snip some code>

So your point is what? You have not changed the computations, they're
still wrong.

Nov 15 '05 #8
Bore Biko wrote:

Dear,

I have function that transform time from secconds
to string formated as dd:hh:mm:ss
(days:hours:minutes:seconds), but it doesent
work correctly...
You should probably give a better description of the problem than
"doesn't work correctly". For example, given input X, I get output
Y when it should have been Z" is better.
Here s code compiled with gcc [...] void transform(char t[], int time){
int days, hours, minuts, seconds, i, j, k;
days=time/(24*3600);
i=time%(24*3600);
time=time/(24*3600)+i;
hours=(time/3600);
i=time%3600;
time=time/3600+i;
minuts=time/60;
i=time%60;
time=time/60+i;
seconds= time % 60;
sprintf(t,"%d:%d:%d:%d",days, hours, minuts, seconds);
}


Others have pointed out problems with your code already. However, I
would like to point out a "cleaner" way of doing this.

Work "backwards".

That is, calculate the seconds first:

seconds = time % 60;
time /= 60;

Then minutes:

minutes = time % 60;
time /= 60;

And so on.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #9
Robert Bralic wrote:

"Eric Sosman" <er*********@sun.com> wrote in message
news:dg**********@news1brm.Central.Sun.COM...


Bore Biko wrote On 09/20/05 17:22,:
Dear,


Sshh! Not in front of the children!

I don't know what to put a young lady who is English teacher
told me that is usualy put: "Dear"

That's known as a "salutation", although missing part, it should have
said something like, "Dear group members". This should be included in
formal letters. Traditionally usenet message do not have salutations.
You can leave it out without offending the vast majority of readers.

Brian
Nov 15 '05 #10
On Wed, 21 Sep 2005 09:22:31 +0200, in comp.lang.c , "Robert Bralic"
<ro*********@yahoo.co.uk> wrote:

"Eric Sosman" <er*********@sun.com> wrote in message
news:dg**********@news1brm.Central.Sun.COM...


Bore Biko wrote On 09/20/05 17:22,:
> Dear,
Sshh! Not in front of the children!

I don't know what to put a young lady who is English teacher
told me that is usualy put: "Dear"


You need "Dear <persons name>. " Just "Dear" by itself is a term you
typically only use when referring to your spouse or partner.....
days=time/(24*3600);


it'll still overflow on a platform with 16-bit ints.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #11

"Default User" <de***********@yahoo.com> wrote in message
news:3p************@individual.net...
Robert Bralic wrote:

"Eric Sosman" <er*********@sun.com> wrote in message
news:dg**********@news1brm.Central.Sun.COM...


Bore Biko wrote On 09/20/05 17:22,:
> Dear,

Sshh! Not in front of the children!

I don't know what to put a young lady who is English teacher
told me that is usualy put: "Dear"

That's known as a "salutation", although missing part, it should have
said something like, "Dear group members". This should be included in
formal letters. Traditionally usenet message do not have salutations.
You can leave it out without offending the vast majority of readers.

Brian


Thanks for involving me in better comunication.
Robert...!!
Nov 15 '05 #12

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

Similar topics

3
by: .: DeVa :. | last post by:
I made a script for loging into members area and have one problem im using functions() to declare procedures to do... And when i want to use some variable that is out of the function it doesent...
3
by: Piotre Ugrumov | last post by:
I have declared some function virtual, but when I call this function I have errors. In a class Simulator I have defined this: Nave *navi; The virtual function is the function modifica(). I call...
3
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any...
2
by: Steve Drake | last post by:
All, I have a servicedcomponet that works fine if its activated as library app, but doesn't work if its activated as a server component. I get the following error : ...
2
by: Armin | last post by:
Hello I want get the Screen Coordinates of a Control. I do this with Control.PointToScreen. vlob_Point = Me.PointToScreen(New System.Drawing.Point(0, 0)) This works fine with a Single...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
3
by: paul | last post by:
HI! I have being to add the following as part of a function but it just will not work as is but I don't know why, can someone point out why. This opens up a popup window for a popup detection...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
1
by: finerrecliner | last post by:
what i'm trying to accomplish: user clicks button. then can click 2 more times anywhere on the page and display his mouse coordinates. after those 2 clicks, go back to normal mouse behavior the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.