Calculate Pace (Running Program) - # 2
I'm now into week # 14 of study, in which Classes and Objects have been introduced. (Studying from the book, C++ Primer Plus Fifth Edition, by Stephen Prata - about to commence chapter 12). In order to cement knowledge learned to date I decided to modify a program I wrote back in week 5. That program written calculates the pace of a run which is outputted as minutes per kilometre. It is only a one source code file program containing a few functions.
I've modified it, maybe modify is the wrong word, completely re-written it. This time basing the functionality upon Classes and Objects and purposely including multiple source code files (headers and definitions). Also in the process including a user decision as to the base unit (kilometres or miles).
The concept of Classes and Objects, though I understand them, opened my eyes more so towards the end of this little project. Originally, I only planned to report the pace in the units as given by user input, ie either 'mins per mile' or 'mins per km'. As I was finishing up I thought I might as well display the opposite or converted value. To my surprise, very few lines of code were required, the main bit being just another constructor passing in a reference of the user inputted details.
Without further ado, here's the code. I'll start with the main() program, hopefully you'll note that it's clean and easy to read, that's because all the work is done via the other files.
I'm open to any constructive criticism or suggestions and without further ado (again), here's the code.
Name: main.cpp
Copyright:
Author: Steven Taylor
Date: 21/02/08 21:25
Description: Main program calling classes to work out pace of a run
*/
#include <iostream>
#include "run01.h"
#include "functions.h"
using std::cout;
using std::cin;
int main()
{
char active = 'y';
run session;
cout << "Enter details (q to quit):\n";
while (active == 'y' || active == 'Y')
{
cin >> session;
run convert(session);
cout << "\n" << session << " or " << convert << "\n\n";
cout << "Another session (y or n) ";
cin >> active;
// anything but Y or y assumes NO
}
// exit routine
cout << "\n\n...Press ENTER to Exit System...";
clear_buffer();
cin.get();
return 0;
}
File # 2
Name: run01.h
Copyright:
Author: Steven Taylor
Date: 21/02/08 14:10
Description: Header file for a run
*/
#ifndef RUN01_H_
#define RUN01_H_
class run
{
private:
static const int SECS_IN_MIN = 60;
static const int MINS_IN_HOUR = 60;
static const int SECS_IN_HOUR = 3600;
static const double KM_PER_MILE = 1.60934;
static const double MILES_PER_KM = 0.621371;
// inputted values
double dd; // distance (km or miles)
int hh; // hour component of time duration
int mm; // minutes component of time duration
int ss; // seconds component of time duration
char unit; // k = kilometre (default), m = miles
// calculated values
int mins;
int secs;
public:
run(); // default constructor
run(double d, int h, int m, int s, char u = 'k');
run(double d, int m, int s, char u = 'k');
~run();
run(const run &r); // converts to other unit of distance
void set_run(double d, int h, int m, int s, char u = 'm');
void set_run(double d, int m, int s, char u = 'k');
void calculate_pace();
void show_values();
friend std::ostream & operator<<(std::ostream &os, const run &r);
friend std::istream & operator>>(std::istream & is, run &r);
};
#endif
File # 3
Name: run01.cpp - definition file
Copyright:
Author: Steven Taylor
Date: 21/02/08 14:56
Description:
*/
#include <iostream>
#include "run01.h"
#include <fstream>
#include "functions.h"
run::run()
{
dd = 0.0;
hh = mm = ss = mins = secs = 0;
unit = 'k';
}
run::run(double d, int h, int m, int s, char u)
{
dd = d;
hh = h;
mm = m;
ss = s;
unit = u;
calculate_pace();
}
run::run(double d, int m, int s, char u)
{
dd = d;
hh = 0;
mm = m;
ss = s;
unit = u;
calculate_pace();
}
run::run(const run &r)
{
if (r.unit == 'k')
{
unit = 'm';
dd = r.dd / KM_PER_MILE;
}
else
{
unit = 'k';
dd = r.dd / MILES_PER_KM;
}
hh = r.hh;
mm = r.mm;
ss = r.ss;
calculate_pace();
}
run::~run()
{}
void run::set_run(double d, int h, int m, int s, char u)
{
dd = d;
hh = h;
mm = m;
ss = s;
unit = u;
calculate_pace();
}
void run::set_run(double d, int m, int s, char u)
{
dd = d;
hh = 0;
mm = m;
ss = s;
unit = u;
calculate_pace();
}
void run::calculate_pace()
{
int total_secs = (hh * SECS_IN_HOUR) + (mm * SECS_IN_MIN) + ss;
int secs_per_unit = ((double(total_secs) / dd) + 0.5);
if (secs_per_unit > 0)
{
mins = secs_per_unit / MINS_IN_HOUR;
secs = secs_per_unit % MINS_IN_HOUR;
}
}
void run::show_values()
{
std::ofstream fout;
fout.open("debug.txt");
fout << "hh = " << hh << " mm = " << mm << " ss = " << ss << " unit = " << unit << "\n";
fout << "mins = " << mins << " secs = " << secs << "\n";
}
std::ostream & operator<<(std::ostream &os, const run &r)
{
os << "Pace : ";
os << r.mins << ":";
if (r.secs < 10)
os << "0";
os << r.secs;
if (r.unit == 'k')
os << " min/km";
else
os << " min/mile";
return os;
}
std::istream & operator>>(std::istream & is, run &r)
{
std::cout << "Unit of distance <k>ilometres or <m>iles: ";
if (is >> r.unit)
{
switch (r.unit)
{
case 'M':
case 'm':
r.unit = 'm';
break;
default :
r.unit = 'k';
}
}
std::cout << "Distance ";
if (r.unit == 'k')
std::cout << "(km): ";
else
std::cout << "(miles): ";
while (!(is >> r.dd))
{
std::cout << "-Distance : ";
clear_buffer();
}
std::cout << "Hours: ";
while (!(is >> r.hh))
{
std::cout << "\n-Hours: ";
clear_buffer();
}
std::cout << "Minutes: (0 to 59): ";
while (!(is >> r.mm) || r.mm >= r.MINS_IN_HOUR)
{
std::cout << "\n-Minutes: (0 to 59): ";
clear_buffer();
}
std::cout << "Seconds: (0 to 59): ";
while (!(is >> r.ss) || r.ss >= r.SECS_IN_MIN)
{
std::cout << "\n-Seconds: (0 to 59): ";
clear_buffer();
}
r.calculate_pace();
return is;
}
File # 4
Name: functions.h
Copyright:
Author: Steven Taylor
Date: 22/02/08 16:21
Description: General functions file
*/
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
void clear_buffer();
#endif
File # 5
Name: functions.cpp
Copyright:
Author: Steven Taylor
Date: 22/02/08 16:24
Description: General function(s) definitions
*/
#include <iostream>
#include "functions.h"
using std::cin;
void clear_buffer()
{
cin.clear();
while (cin.get() != '\n')
continue;
}
So that's it, what do you think?
| Attachment | Size |
|---|---|
| Pace-Class-Run.zip | 2.76 KB |
- Printer-friendly version
- Login or register to post comments
- 375 reads


Recent comments
4 hours 30 min ago
1 day 4 hours ago
1 day 10 hours ago
1 day 20 hours ago
1 day 20 hours ago
3 weeks 6 days ago
3 weeks 6 days ago
4 weeks 5 hours ago
10 weeks 1 day ago
11 weeks 5 days ago