Troubleshooting C Program - Format Must Be HHMMSS

  • Thread starter sg001
  • Start date
In summary, the code has two scanf calls in a row, and the first one fails because the time parameter is not numeric.
  • #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
 
Physics news on Phys.org
  • #2
The code you show has two scanf calls in a row.
 
  • #3
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.
 

Related to Troubleshooting C Program - Format Must Be HHMMSS

1. What does the error message "Format must be HHMMSS" mean?

The error message "Format must be HHMMSS" means that the format of the code in the C program is incorrect and does not match the required format of HHMMSS (hours, minutes, seconds). This can cause the program to not run properly or produce unexpected results.

2. How can I fix the "Format must be HHMMSS" error?

To fix the "Format must be HHMMSS" error, you will need to review your code and make sure that you are using the correct format of HHMMSS. This means ensuring that the hours, minutes, and seconds are in the correct order and correctly formatted with leading zeros if necessary.

3. What are some common causes of the "Format must be HHMMSS" error?

Some common causes of the "Format must be HHMMSS" error include using the wrong format for the time in the code, such as using a 12-hour format instead of a 24-hour format, or not including the required colons between the hours, minutes, and seconds.

4. Can I use other time formats besides HHMMSS in my C program?

Yes, you can use other time formats besides HHMMSS in your C program. However, if the program is expecting a specific format, such as HHMMSS, it is important to make sure that your code is using the correct format to avoid the "Format must be HHMMSS" error.

5. How can I prevent the "Format must be HHMMSS" error from occurring?

To prevent the "Format must be HHMMSS" error from occurring, always make sure to carefully review your code and use the correct format for the time. It may also be helpful to use a debugger or other tools to identify and fix any formatting errors before running the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
975
  • Engineering and Comp Sci Homework Help
Replies
3
Views
979
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
772
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top