Thread: Help Write This Program Plz

  1. #1
    Unregistered
    Guest

    Unhappy Help Write This Program Plz

    One enters elapsed times for each runner given in units of minutes, seconds, and hundredths. In a 5000 meter race, elapsed times are entered at the one and two mile marks. These elapsed times are then used to compute "splits" for each part of the race; that is, the time it took a runner to run each of the three race segments. Write a complete program that will accept as input three times given in units of minutes, seconds, and hundredths, and then produce output that includes the split for each segment. Typical input would be:

    Runner Number 234

    Mile times: 1 5:34.22

    2 11:21.67

    Finish Time: 17:46.85

    Typical output would be

    Runner number 234

    Split One 5:34.22

    Split Two 5:47.45

    Split Three 6:25.18

    Finish Time 17:46.85

    This problem has our whole c++ class stumped.
    Our problem is that c++ will not understand the
    time in that format because of the COLON
    PLEASE HELP

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I assume if you had the input in separate hour, minute, and second parameters the calculations wouldn't be a problem. If the input is as a string, then, the trick will be convert the information in the string into the three separate variables. This process is called parsing. The way to do it depends on what you already know, what resources you are limited to, and what type of string you are using.

    Irrespective of what type of string you are using you can write your own function to search for the separating colon and period by searching the underlying char array one char at a time using a loop. Each char you find is placed in a separate char array until you find the colon or period. when the colon or period is found then place a nullterminating char in the char array and move on the the next char array.

    If you don't want to write your own parsing function then there are several availabe for your use. strtok() can be used on char arrays, and find() can be used on strings of type string.

    You might even want to look into use of strstreams, but I am not as familiar with those.

    Once you have separated the original string into the three separate strings you can convert them into ints by using atoi().

    Once you have all the variables as ints you can do the math.

    Then you can print out the results using ints separated by appropriate delimiters or put it all back in a string again.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can make much more efficient code in this situation by taking advantage of the fact that atoi stops at the first non-numeric input.So you get the input, do an atoi on it,use strchr() to find the colon. advance that pointer by 1 element. atoi() again... etc.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 5:34.22
    Well if you're allowed to use sscanf (yes I know this is a C function and this is C++, but you can still call it), you could do this

    char *buff = "5:34.22";
    sscanf( buff, "%d:%d.%d", &min, &sec, &hundredths );

    Once you've set buff to point to the start of the time, sscanf will extract all the numbers for you in one go.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using While Loop to write a program
    By Cyberman86 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 12:52 PM
  2. Program to write to memory map addresses
    By modest in forum C Programming
    Replies: 15
    Last Post: 06-03-2008, 03:20 PM
  3. Hints to write a program
    By Bnchs400 in forum C++ Programming
    Replies: 28
    Last Post: 04-05-2006, 05:35 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. plz can u help me with this program
    By itcs in forum C Programming
    Replies: 6
    Last Post: 07-31-2003, 08:56 AM