Journey time prog 1 minute wrong

This is a discussion on Journey time prog 1 minute wrong within the C Programming forums, part of the General Programming Boards category; I wrote a program for an assignment to calculate journey times. There is a problem since sometimes (not allways) the ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Journey time prog 1 minute wrong

    I wrote a program for an assignment to calculate journey times. There is a problem since sometimes (not allways) the journey time returned is 1 off what its meant to be. I tried several different things but cnat seem to find why

    Code:
    #include <stdio.h>
    #include <conio.h>
    
                   /*TASK 5: JOURNEY TIMES*/                    
    int main()
    {
    //-------------------- DECLARE VARIABLES --------------------------------------//
    
              float departure_time, arrival_time;                                                              
              int departure_hours, departure_minutes;
              int arrival_hours, arrival_minutes;   
              int duration_hours, duration_minutes;
    
    //---------------------- MAIN PROGRAM -----------------------------------------//
    
              printf("--- CALCULATIONS BASED ON A 24 HOUR CLOCK ---\n"); 
    //Enter departure time
              printf("Enter the departure time of your train: ");
              scanf("%f", & departure_time);
              departure_hours = departure_time;     //Copy the float to an int to floor  its value
    
    //Validate departure time based on a 24hr clock 
              while (departure_time - departure_hours > 0.59 || departure_time < 0 || departure_hours > 24 || (departure_hours == 24 && departure_time - departure_hours > 0))         
              {
                    printf("Invalid. Re-enter the departure time of your train: ");
                    scanf("%f", & departure_time);
                    departure_hours = departure_time;            
              }      
    
              departure_minutes = (departure_time - departure_hours)*100;   //Multiply out remaining minutes so they can be stored as an integer    
     
    //Enter arrival time              
              printf("Enter the arrival time of the train: ");
              scanf("%f", & arrival_time); 
              arrival_hours = arrival_time;
              
    //Validate arrival time based on a 24hr clock 
              while (arrival_time - arrival_hours > 0.59 || arrival_time < 0 || arrival_hours > 24 || (arrival_hours == 24 && arrival_time - arrival_hours > 0))         
              {
                    printf("Invalid. Re-enter the arrival time of your train: ");
                    scanf("%f", & arrival_time);
                    arrival_hours = arrival_time;            
              }             
              arrival_minutes = (arrival_time - arrival_hours)*100;
    
              duration_minutes = arrival_minutes - departure_minutes;
    
    //Calculate journey time
              //If there is going to be a negative number of minutes
              if (duration_minutes < 0)
              {
                     duration_minutes = 60 + duration_minutes; //invert minutes on a range of 60 
                     arrival_hours = arrival_hours - 1;        //the journey will now take one less hour
              }
              duration_hours = arrival_hours - departure_hours;
              //If the journey travels through midnight
              if (duration_hours < 0)
                     duration_hours = 24 + duration_hours;     //invert hours on a scale of 24      
    //Display journey time         
              printf("The journey will take %i hours and %i minutes", duration_hours, duration_minutes);          
              
                                
              getchar();
              getchar();
              return 0;
    }
    Also a friend of mine said that theres a way to turn a float input by scanf into two seperate integers. EG: 23.45 becomes 23 and 45. If its possible could someone let me know haw that would be done, it would have made this prog a bit simpler.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    It would be considerably easier if you did
    scanf( "%d.%d", &hours, &minutes );

    From there, it's easy maths to calculate a minute value from both times.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yes. That is better. I rewrit the prog, so it now looks like:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
                      /* MIKE GRUNDEL */
                   /*TASK 5: JOURNEY TIMES*/                    
    int hours, minutes;
    
    int main()
    {
              void INPUT_TIME();
    //-------------------- DECLARE VARIABLES --------------------------------------//
                                                                
              int departure_hours, departure_minutes;
              int arrival_hours, arrival_minutes;   
              int duration_hours, duration_minutes;
    
    //---------------------- MAIN PROGRAM -----------------------------------------//
    
              printf("--- CALCULATIONS BASED ON A 24 HOUR CLOCK ---\n"); 
    //Enter departure time
              printf("Enter the departure time of your train: ");
              INPUT_TIME(departure_hours, departure_minutes);   
              departure_hours = hours;
              departure_minutes = minutes;
    //Enter arrival time
              printf("Enter the arrival time of your train: ");
              INPUT_TIME(arrival_hours, arrival_minutes); 
              arrival_hours = hours;
              arrival_minutes = minutes;          
              
    //Calculate journey time
    
              duration_hours = arrival_hours - departure_hours;
              duration_minutes = arrival_minutes - departure_minutes;
    
              //If there is going to be a negative number of minutes
              if (duration_minutes < 0)
              {
                     duration_minutes = 60 + duration_minutes; //invert minutes on a range of 60 
                     arrival_hours = arrival_hours - 1;        //the journey will now take one less hour
              }
              duration_hours = arrival_hours - departure_hours;
              //If the journey travels through midnight
              if (duration_hours < 0)
                     duration_hours = 24 + duration_hours;     //invert hours on a scale of 24      
    //Display journey time         
              printf("The journey will take %i hours and %i minutes", duration_hours, duration_minutes);          
                              
              getchar();
              getchar();
              return 0;
    }
    
    //-----------------------FUNCTIONS----------------------------//
    void INPUT_TIME()
    {
              scanf("%i: %i", &hours, &minutes);
              while (minutes > 59 || minutes < 0 || hours > 24 || hours < 0 || (hours == 24 && minutes > 0))         
              {
                    printf("Invalid. Re-enter the time: ");
                    scanf("%i:%i", &hours, &minutes); 
              }           
    }
    One problem is that I dont know how to return variables from a void functon. I did it once before and I remember that you needed to use a * pointer thingy. It would be cool if someone could give me an example of how this program could be run without having to have "hours" and "minutes" as globals.

    Another problem with this prog is if the time is not entered in HH:MM format, with the colon, the while loop for the validation runs infinitely. How could I stop that from happening?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It would be cool if someone could give me an example of how this program could be run without having to have "hours" and "minutes" as globals.
    Pointer example:
    Code:
    #include <stdio.h>
    
    void increment(int *x);
    
    int main(void) {
        int x = 1;
    
        printf("%i\n", x);
        increment(&x);
        printf("%i\n", x);
    
        return 0;
    }
    
    void increment(int *x) {
        *x += 1;
    }
    
    /* output
    1
    2
    */
    Another problem with this prog is if the time is not entered in HH:MM format, with the colon, the while loop for the validation runs infinitely. How could I stop that from happening?
    If I may: http://board.theprogrammingsite.com/viewtopic.php?t=223
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Smile

    Thanks. Seems I forgot to include & when passing my parameters in o_0. The scanf thing will be useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  2. How to add some time to a prog
    By ErionD in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2002, 05:00 AM
  3. how to make the prog. read time from pc clock
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2002, 03:50 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 10:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21