0

This is the language C and as the title says I am trying to make it so that the users put in seconds and that gets converted to weeks, days, hours, minutes and seconds like this:

Input: x seconds

Output: Weeks x, days x, hours 59, minutes 59, seconds 59 - Just an example.

Code:

int main(void) {
    int w, d, h, m, s;
    printf("Please enter amount of seconds: " );
    //%d er placeholder og &s er den enhed som benyttes - i dette tilfælde sekunder. 
    scanf("%d",&s);
    //1 week = 7 days.
    w = d / 7;
    d = d % 7;
    //1 day = 24 hours.
    d = h / 24;
    h = h % 24;
    //1 hour = 60 minutes.
    h = m / 60;
    m = m % 60;
    //1 minute = 60 seconds.
    m = s / 60;
    s = s % 60;
    printf("\nWeeks: %d", h);
    printf("\nDays: %d", h);
    printf("\nHours: %d", h);
    printf("\nMinutes: %d", h);
    printf("\nSeconds: %d", h);
    
    return 0;
}

My question: When I try compiling this, it just prints out 70019 in all the fields regarding of input - I suppose this is the last memory used? So obviously something is wrong, but I ain't good enough to figure it out :/

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 3
    What's the question? – SergeyA Sep 08 '21 at 17:58
  • 3
    `d = h / 24;` - `h` is uninitialized at this point. – Eugene Sh. Sep 08 '21 at 18:00
  • 7
    You are working in the wrong direction: from seconds you get minutes -> hours -> days -> weeks. C isn't like a spreadsheet that updates all references and `w = d / 7;` does not establish some kind of dynamic relationship. – Weather Vane Sep 08 '21 at 18:02
  • 2
    Oh, so I apparantly work from weeks -> days -> hours -> minutes -> seconds. So I have to work from the other way around - this makes sense. – Jonas Schøn Sep 08 '21 at 18:04
  • Welcome! Please don't make rolling progress updates to the code that is posted. – Weather Vane Sep 08 '21 at 18:08
  • I tried changing the code now, but now it just output 14. Idk what I am missing, is it because I havent defined number range in each? - Okay sorry, weather vane. Idk how this forum quite works yet! – Jonas Schøn Sep 08 '21 at 18:10
  • 1
    You have used `h` for *every* output no matter what! The site isn't intended to be so much an interactive tutorial, as for asking specific questions about C. Aside: as a matter of typical practice it is usual to place the newline at the other end of the output, so `printf("Weeks: %d\n", w);` Note that is `w` not `h`. – Weather Vane Sep 08 '21 at 18:11
  • Side note: Don't be so sure of `//1 minute = 60 seconds.` See [leap seconds](https://en.wikipedia.org/wiki/Leap_second). For example, note how [this documentation page for `struct tm;`](https://en.cppreference.com/w/c/chrono/tm) shows `tm_sec` being allowed to have 61 or 62 seconds depending on the C++ Standard Revision. – user4581301 Sep 08 '21 at 18:24
  • Thank you so much Weather Jane. The type of site you mentioned, do you know of any? User4581301: We have not learned about that at university, I just enrolled. – Jonas Schøn Sep 08 '21 at 18:28
  • Leap seconds are more related to a calendar passage of time, but here the units of time are no bigger than *week*. Once you involve months, that relates to a calendar measurement of time and brings in leap years and leap seconds. – Weather Vane Sep 08 '21 at 18:34
  • ...if it were impossible to forget initializing variables, half of the reasons for having this site would cease to exist. – ryyker Sep 08 '21 at 18:35
  • @user4581301 Nicely erudite comment about leap seconds, but in actual practice — they're nothing to worry about. Unix and Linux don't have proper handling for leap seconds and probably never will. `tm_sec` will probably never hit 60 (and will certainly never hit 61!). See https://stackoverflow.com/questions/48846521 . – Steve Summit Sep 08 '21 at 19:35
  • @SteveSummit I have had it bite me a few times when that extra second showed up at exactly the wrong time or data being shared between systems appeared inconsistent because one tracked and the other did not track leap seconds. The sooner we ditch the suckers, the better. I dropped that as an informative and admit it should be a non-issue most (and that's a dominating most) of the time. Plus any instructor who builds handling them into a introductory programming assignment is a bastard. – user4581301 Sep 08 '21 at 19:53
  • @user4581301 Out of curiosity, which systems have you used that did handle leap seconds? (I have a special interest in this subject. Many people consider it impossible to handle leap seconds, so I'm especially interested in counterexamples.) – Steve Summit Sep 08 '21 at 19:55
  • @SteveSummit Perhasp a good question for some SE site? – chux - Reinstate Monica Sep 08 '21 at 19:58
  • One was an earthquake tracking system. I don't think we're using it anymore. OS may have been QNX, but that was probably just the OS for the controller. – user4581301 Sep 08 '21 at 21:08
  • I'm surprised posts like [this one](https://stackoverflow.com/questions/11233602/what-will-be-the-value-of-uninitialized-variable?noredirect=1&lq=1) haven't been linked. Uninitialized variables are a clear issue with the code posted here, and they've been addressed many times on SO. – Chris Sep 08 '21 at 21:23

1 Answers1

2

So obviously something is wrong, but I ain't good enough to figure it out :/

The big mistake is not using a good compiler with all warnings enabled.
w = d / 7; could report something like

> warning: 'd' is used uninitialized in this function [-Wuninitialized].

My compilation had 4 warnings. Save time, be more productive. Enable all warnings.


Then re-order computations and use error checking.

// scanf("%d",&s);
if (scanf("%d",&s) == 1) {
  int m = s / 60;
  s %= 60;

  int h = m / 60;
  m %= 60;

  int d = h / 24;
  h %= 24;

  ...
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I tried installing a compiler for VS, but our professor told us to get used to GCC (GNU Compiler Collection) as it would be beneficial later. – Jonas Schøn Sep 08 '21 at 19:58
  • @JonasSchøn Maybe use `gcc -std=c11 -O0 -g3 -pedantic -Wall -Wextra -Wconversion -c -fmessage-length=0 ...`. Note that `-Wall` is not _all_, just _some_. – chux - Reinstate Monica Sep 08 '21 at 19:59