Return to the BobbyGriggs.com C++ Nook


Chapter 3 - Split Times
© 1998 by Bobby Griggs.



// Program name:  Runner

// Purpose:  To compute split times for a runner in a 5k race

// Info:  Required program for Chapter 3



#include < iostream.h >	// necessary for cout and cin

#include < stdio.h >	// necessary for getchar

#include < iomanip.h >	// necessary for ios settings



void main()

{

	int runner;			// variable for runner number

	int min1,sec1,hun1;	// variables for first split

	int min2,sec2,hun2; // variables for second split

	int min3,sec3,hun3; // variables for finish time

	int finmin,finsec,finhun; // hold finish time

	int time1,time2,time3;  // convert to hundredths



	// set ios functions

	cout << setiosflags(ios::left);



	// Accept entry of splits and convert all times to seconds

	cout << "Enter the runner's number ... ";

	cin >> runner;



	cout << endl << "Enter split times in minute, seconds, and hundredths." << endl << endl;

	cout << "Use the following format:  xx xx xx" << endl << endl;

	

	cout << "Enter time at end of mile 1 ... ";

	cin >> min1 >> sec1 >> hun1;

	time1 = (min1 * 60 * 100) + (sec1 * 100) + hun1;

	

	cout << endl << "Enter time at end of mile 2 ... ";

	cin >> min2 >> sec2 >> hun2;

	time2 = (min2 * 60 * 100) + (sec2 * 100) + hun2;



	cout << endl << "Enter time at end of 5k run ... ";

	cin >> min3 >> sec3 >> hun3;

	time3 = (min3 * 60 * 100) + (sec3 * 100) + hun3;



	// hold finish time for output later

	finmin = min3;

	finsec = sec3;

	finhun = hun3;





	// Compute split times

	time3 = time3 - time2;

	time2 = time2 - time1;



	min1 = time1 / 6000;

	time1 = time1 % 6000;

	sec1 = time1 / 100;

	time1 = time1 % 100;

	hun1 = time1;



	min2 = time2 / 6000;

	time2 = time2 % 6000;

	sec2 = time2 / 100;

	time2 = time2 % 100;

	hun2 = time2;



	min3 = time3 / 6000;

	time3 = time3 % 6000;

	sec3 = time3 / 100;

	time3 = time3 % 100;

	hun3 = time3;



	// Output split times to the monitor.

	cout << endl << endl << endl << endl;

	cout << setw(20) << "Runner" << runner << endl << endl;

	cout << setw(20) << "Split one" << min1 << ":" << sec1 << "." << hun1 << endl << endl;

	cout << setw(20) << "Split two" << min2 << ":" << sec2 << "." << hun2 << endl << endl;

	cout << setw(20) << "Split three" << min3 << ":" << sec3 << "." << hun3 << endl << endl;

	cout << setw(20) << "Finish time" << finmin << ":" << finsec << "." << finhun << endl << endl;



	cout << "Hit any key to continue ... " << endl;

	getchar();

	return;

}