hi guys!!

The purpose of the program I am trying to do is to have an input number of 6 digits typed in and this should then be printed as an hour minute second format. for example 123456 would be 12:34:56

This is what I have but it isn't working in the slightest..??

include <stdio.h>

int main(int argc, char * argv []) {

int sixdigits;  



printf("Enter 24 hour time in HHMMSS format:\n");
scanf("%d", &sixdigits);

int hr = (sixdigits/10000);
int min = ((sixdigits/100)-(hr*100));
int sec = (sixdigits-(hr*10000)-(min*100));

if ((hr>=00 && hr<24) && (min>=00 && min<=59) && (sec>=00 && sec<=59)) {
    printf("%d : %d : %d\n", hr, min, sec);
} else { printf("Format must be HHMMSS\n");
}



return 0;

}

Dani AI

Generated

Several replies already touched on compiler/header quirks and integer-size issues (thanks to , and ). A practical gap not covered is handling leading zeros and non-digit input: treating the user entry as text makes validation simpler and preserves numbers like 003000 (00:30:00). Also use zero-padded output so 1:2:3 prints 01:02:03.

A safe, minimal pattern is: read a small string with bounds checking, strip the newline, confirm length is exactly 6 and every character is a digit, parse three two-digit fields, validate ranges, then print with %02d:%02d:%02d. Example implementation:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void) {
    char buf[16];
    if (!fgets(buf, sizeof buf, stdin)) return 0;
    buf[strcspn(buf, "\n")] = '\0';
    if (strlen(buf) != 6) { puts("Format must be HHMMSS"); return 0; }
    for (int i = 0; i < 6; ++i)
        if (!isdigit((unsigned char)buf[i])) { puts("Format must be HHMMSS"); return 0; }
    int h, m, s;
    if (sscanf(buf, "%2d%2d%2d", &h, &m, &s) != 3) { puts("Format must be HHMMSS"); return 0; }
    if (h < 0 || h >= 24 || m < 0 || m >= 60 || s < 0 || s >= 60) { puts("Format must be HHMMSS"); return 0; }
    printf("%02d:%02d:%02d\n", h, m, s);
    return 0;
}

Troubleshooting tips: compile with warnings enabled (for example -Wall -Wextra), test edge cases (000000, 235959, 240000 is invalid), and avoid unbounded scanf("%s"). If working on an old compiler with tiny int, prefer fixed-width types (int32_t) or parse as text as shown. If there are still build errors, include the exact compiler and error output for targeted help.

Recommended Answers

All 3 Replies

Your include header file should be preceded with a hash (#)

The minimum range for int is -32767 to +32767. If your compiler (also known as implementation) is at this minimum, int is not large enough to store 6 digits. However, many implementations have a much larger range so that may not be your problem. You may want to look values in <limits.h> to see what range your compiler supports.

This is what I have but it isn't working in the slightest..??

That program worked ok for me using VC++ 2010 Express on Windows 7. If its broken for you then I suspect that pheininger is correct in his assessment. Change int sixdigits to long sixdigits

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.