Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home C++ (The Book) Short Programs


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
    • Basic cin operations
    • Short Programs
      • Create a triangle type pattern
      • Array Solution - Half and half, 10 per line
      • Calculate Pace (Running Program)
      • Calculate Pace (Running Program) - # 2
      • Remove Vowels
      • Remove Vowels - # 2 - String version
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Got it solved This page here
    4 hours 30 min ago
  • Links working
    1 day 4 hours ago
  • Thanks... I may be able to
    1 day 10 hours ago
  • 3306 by default
    1 day 20 hours ago
  • Is this the right place to
    1 day 20 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    4 weeks 5 hours ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 5 days ago

New forum topics

  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
  • index.php?
more

Who's online

There are currently 0 users and 0 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

Calculate Pace (Running Program) - # 2

  • View
  • Revisions
Submitted by Steve on Sat, 23 Feb, 2008 - 11:29
  • C++
  • class
  • distance
  • objects
  • pace
  • time

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?

AttachmentSize
Pace-Class-Run.zip2.76 KB
‹ Calculate Pace (Running Program) up Remove Vowels ›
  • Printer-friendly version
  • Login or register to post comments
  • 375 reads

 Subscribe in a reader

free hit counter


RoopleTheme