Troubleshooting C Program - Format Must Be HHMMSS

  • Thread starter Thread starter sg001
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on troubleshooting a C program designed to read and validate time input in HHMMSS format. The primary issue identified was the presence of two consecutive scanf calls, which caused the program to exit prematurely without executing the intended logic. The solution involved removing the redundant scanf statement, allowing the program to correctly process valid numeric input and display the appropriate error messages for invalid entries.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with input/output functions in C, particularly scanf and printf
  • Knowledge of basic control flow statements, including if and else
  • Concept of modular arithmetic for time calculations
NEXT STEPS
  • Review C programming best practices for input validation
  • Learn about error handling techniques in C programming
  • Explore modular arithmetic applications in time and date calculations
  • Investigate debugging tools and techniques for C programs
USEFUL FOR

Beginner C programmers, students learning about input validation, and developers looking to improve their understanding of time formatting in 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 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
2K