- #1
sg001
- 134
- 0
Homework Statement
for some reason my c program seems to be exiting out before it runs through the full program.
here is my code... it just a simple program that reads in the time in HHMMSS format and then prints an error msg if the input is not valid i.e. non-numeric or out of range i.e. 253243, since in 24 hour time it does not make sense to have hour 25... and so on.
#include <stdio.h>
int main(int argc, char * argv[]){
#define SEC_PER_MIN 60
#define MIN_PER_HOUR 60
#define ERROR_MSG "Format must be HHMMSS"
int sec=0, min=0, hour=0, time;
printf("Enter 24 hour time in HHMMSS format: \n");
scanf("%d",&time);
if(scanf("%d", &time) !=1){
printf("Format must be HHMMSS\n");
return 0;
}
sec=time%100;
time=time/100;
min=time%100;
time=time/100;
hour=time;
if (hour>23 || min>59 || sec>59){
printf("No such time\n");
return 0;
}
else if (hour<0 || min<0 || sec<0){
printf("No such time");
return 0;
}
else {
printf("The time is %d:%d:%d\n",hour, min, sec);
}
return 0;
}
any suggestions on how to fix this?
Homework Equations
The Attempt at a Solution
#include <stdio.h>
int main(int argc, char * argv[]){
#define SEC_PER_MIN 60
#define MIN_PER_HOUR 60
#define ERROR_MSG "Format must be HHMMSS"
int sec=0, min=0, hour=0, time;
printf("Enter 24 hour time in HHMMSS format: \n");
scanf("%d",&time);
if(scanf("%d", &time) !=1){
printf("Format must be HHMMSS\n");
return 0;
}
sec=time%100;
time=time/100;
min=time%100;
time=time/100;
hour=time;
if (hour>23 || min>59 || sec>59){
printf("No such time\n");
return 0;
}
else if (hour<0 || min<0 || sec<0){
printf("No such time");
return 0;
}
else {
printf("The time is %d:%d:%d\n",hour, min, sec);
}
return 0;
}
if I enter a value say aabdsf it prints "Format must be in HHMMSS"
but If enter enter a valid input 123445 it doesn't do anything and the same as if I enter 543443
a non valid numeric input it prints no output.
so it seems it must me ending on the scans line but I don't know why... any help would be super!
Thanks