After inputting a specific time, my minutes and seconds start from 0. Can anyone point out the mistakes in my coding please?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main ()
{
int h=0,m=0,s=0,i;
system("cls");
printf("Please enter a time format in HH:MM:SS\n ");
scanf("%d%d%d",&h,&m,&s);
start:
for(h;h<24;h )
{
for(m;m<60;m )
{
for(s;s<60;s )
{
system("cls");
printf("\n\n\n\t\t\t%d:%d:%d",h,m,s);
if(h<12){printf("AM");}
else {printf("PM"); }
for(double i=0;i<99999999;i )
{i ;
i--;}
}
s=0;
}
m=0;
}
h=0;
goto start;
getch();
return 0;
}
If I input 22:23:32
, it will show to start from 22:0:0
.
CodePudding user response:
The colon separators in your input are causing the scanf
call to fail (after reading the h
value), as they cannot be interpreted as integers (as expected by the %d
format specifiers).
If you know that your time input will always have the two :
characters separating the hours, minutes and seconds values, then you can include those in the format string you pass to scanf
– which will then look for (and skip) exactly those characters between the integer inputs.
Also, you should get into the habit of always checking the return value of scanf
, to see if it successfully read the required number of fields:
//...
int check = scanf("%d:%d:%d", &h, &m, &s);
if (check != 3) { // Failed to read three integers
printf("Invalid input!\n");
return 1;
}
//...
CodePudding user response:
You want to fix your format string to match your input, and check the return value from scanf()
to ensure you actually read the data you expected. You probably want to print h % 12
as you use AM/PM. I suggest you use sleep instead of the busy loop:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main () {
printf("Please enter a time format in HH:MM:SS\n ");
int h, m, s;
if(scanf("%d:%d:%d",&h,&m,&s) != 3) {
printf("scanf failed\n");
return 1;
}
for(;;) {
for(;h<24;h ) {
for(;m<60;m ) {
for(;s<60;s ) {
printf("\n\n\n\t\t\td:d:d %s",
h % 12,m,s,h < 12 ? "AM" : "PM");
sleep(1);
}
s=0;
}
m=0;
}
h=0;
}
}
and example run:
Please enter a time format in HH:MM:SS
13:2:3
01:02:03 PM
01:02:04 PM
The next step would be to setup a timer at regular intervals as sleep()
will skew when it's run sufficiently long. Use strptime()
and strftie()
if they are available to you.
CodePudding user response:
With thanks to @AllanWind for the correct user input code, there's the following that doesn't have quite the same level of indent and has been adapted to a Windows environment.
#include <stdio.h>
#include <windows.h>
int main () {
printf( "Enter a time (format HH:MM:SS)\n ");
int h, m, s;
if( scanf( "%d:%d:%d", &h, &m, &s ) != 3 ) {
printf("scanf failed\n");
return 1;
}
for( int ds = ((h*60) m)*60 s; ;ds = (ds 1)%(24*60*60) ) {
printf( "d:d:d\n", ds/(60*60), (ds/60)`, ds` );
Sleep( 1000 ); // NB: Uppercase func name and time in mS...
}
return 0;
}
23:59:57
23:59:58
23:59:59
00:00:00
00:00:01
00:00:02
The program should give the processor some exercise through calculating the quotients and modulo remainders from 86400 seconds per day... With a tiny tweak to the hours value, the counter could be left to simply count up, up for the next 136 years (as an unsigned 4byte integer.)