Help with Time? Please....

This is a discussion on Help with Time? Please.... within the C++ Programming forums, part of the General Programming Boards category; I run cross country and have made a program to calculate the splits for my times, for instance if I ...

  1. #1
    RunXC410
    Guest

    Wink Help with Time? Please....

    I run cross country and have made a program to calculate the splits for my times, for instance if I run 3 miles in 17:33 I would put in the minutes and seconds and it would tell me how long it took me to run per mile.

    Currently my math function that I wrote, takes in minutes and seconds as seperate int variables and performs some really long and drawn out math on them. This works, but sometimes my split times are only accurate within 2 seconds or so because of the int varables, I couldn't figure out how to do it with double variables.

    I know about CTime but do not know how to use it, could you please tell me how to declare a CTime variable with minutes = 17 and seconds = 30, then divide that by three and store in a new variable for output, or at least tell me how to divide minutes and seconds precisley, like up to two decimals.

    Thanks,
    RunXC410.

  2. #2
    alpha_guest
    Guest
    have you tried to input them as doubles and then store it back into a double, because if you have the minutes and seconds and ints and try to store them into a double, the answer will still be an int.

    alpha

    *I need to log in

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Why not convert the minutes and seconds into seconds, divide by three (or whatever), then convert back? I would think this would be pretty easy. You'll need to make your seconds always a double though so you can store the decimal place when you convert back.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    By the way, I'm guessing from your login name that you run a mile in 4:10 - if true, thats pretty fast

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Convert your running time to seconds, then do the calculations.
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct TimeType
    {
       int min;
       int sec;
    };
    
    int main(void)
    {
       int miles;
       TimeType running_time;
       TimeType time_per_mile;
       int mile_time_in_sec, time_in_sec;
    
       cout << "How many miles?";
       cin >> miles;
       cout << "Enter your time in minutes and seconds";
       cin >> running_time.min >> running_time.sec;
    
       time_in_sec = running_time.min * 60 + running_time.sec;
       mile_time_in_sec = time_in_sec / miles;
       time_per_mile.min = mile_time_in_sec / 60;
       time_per_mile.sec = mile_time_in_sec % 60;
    
       cout << "running time:" << running_time.min << " minutes " <<
          running_time.sec << " seconds." << endl;
       cout << "time per mile:" << time_per_mile.min << " minutes " <<
          time_per_mile.sec << " seconds." << endl;
    
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

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