1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Having trouble with c

  1. Aug 11, 2012 #1
    1. The problem statement, all variables and given/known data

    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?


    2. Relevant equations



    3. 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
     
  2. jcsd
  3. Aug 11, 2012 #2

    Filip Larsen

    User Avatar
    Gold Member

    The code you show has two scanf calls in a row.
     
  4. Aug 11, 2012 #3

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook