Troubleshooting C Program - Format Must Be HHMMSS

  • Thread starter Thread starter sg001
  • Start date Start date
AI Thread Summary
The C program aims to read time in HHMMSS format and validate the input. The user reported that the program exits prematurely without processing valid inputs. The issue was identified as having two consecutive scanf calls, which caused confusion in input handling. After correcting this, the program successfully validates and displays the time or prints an error message as needed. The discussion highlights the importance of understanding input functions in C programming.
sg001
Messages
134
Reaction score
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
 
Physics news on Phys.org
The code you show has two scanf calls in a row.
 
Filip Larsen said:
The code you show has two scanf calls in a row.


Yep that fixed it., thanks...

I'm new to C and its taking me a while to understand the basic logics of it... so thanks for helping.
 

Similar threads

Replies
3
Views
1K
Replies
1
Views
10K
Replies
12
Views
2K
Replies
8
Views
1K
Replies
12
Views
2K
Replies
15
Views
2K
Replies
3
Views
1K
Replies
4
Views
1K
Back
Top