Code
New Website - Prime 357
Submitted by Steve on 6 April, 2008 - 17:36.I've not been blogging much lately and for the last two weeks it's just been a weekly wrap-up. Most of my time has been spent 1. actually running and 2. designing my new website.
My new website (http://prime357.org) went live on Wednesday 2 April 2008. Prime357 caters for technical computer stuff, stuff that was creeping into this blog.
Since the go-live date I've been fiddling with all sorts of settings in order to re-direct all hits pertaining to Drupal and C++ to Prime357, and it works (the re-direct).
In time I'll remove the menu buttons above pertaining to Drupal & C++. That's not an overly urgent job, click on them and a re-direction occurs to Prime357 anyway.
I'll keep this blog purely for my running and other personal stuff but not geekie stuff.
- Steve's blog
- Add new comment
- 166 reads
- Email this page
Remove Vowels - # 2 - String version
Submitted by Steve on 3 March, 2008 - 16:03.I've modified the Remove Vowels program to now account for string use instead of straight out C-style string. The purpose of the program hasn't changed and that is to accept user input of a few words and strip out all the vowels. Real earth shattering stuff.
Additional to the previous version is that both upper and lowercase characters are now accounted for.
In order to reduce clutter at the end of the main() function, I reduced the exiting procedure to a function 'keep_window_open()'. On some Windows setup, it's a real pain that when running the program from the IDE the command prompt window closes automatically at the conclusion of the program. This function addresses that. If it's not needed simply comment it out.
Here's the code:
// RemoveVowels-4.cpp -- Remove vowels from an inputted string // Steven Taylor // 16 Jan, 2008. // 3 Mar, 2008 : demonstrate using type string #include <iostream> #include <string> #include <cctype> // tolower() function using namespace std; const string VOWEL_IS = "aeiou"; // for caps - see use of tolower() function below. string strip(const string &str); bool isvowel(const char c); void keep_window_open(); int main() { string str; cout << "Enter a string, any old string of words:\n"; getline(cin, str); cout << "\n\nThe user input without vowels :\n" << strip(str) << endl; keep_window_open(); // less clutter. return 0; } string strip(const string &str) { int index = 0; int length = str.size(); char temp[length]; for (int i = 0; i < length; i++) { if (!(isvowel(tolower(str[i])))) { temp[index] = str[i]; index++; } } temp[index] = '\0'; return (string)temp; // return temp; also works. } bool isvowel(const char c) { for (int j=0; j < 5; j++) { if (c == VOWEL_IS[j]) return true; } return false; } void keep_window_open() { // only needed if the command prompt window doesn't // stay open. Using a function - assists // with readability of the main() program // that is, less clutter. cout << "\n\n...Press ENTER to Exit System..."; cin.clear(); while (cin.get() != '\n') continue; cin.get(); }
I'm open to suggestions, good, bad or indifferent. Are there any other ways to achieve the purpose of this program? I'm sure there are. Whether they are more efficient or not is not the point. The point is that it gets the mind ticking considering options and alternatives. I know for one thing, I haven't explored all the std::string functions.
Consider this. If you're next assignment is along the lines of, 'check a user inputted string and remove all the characters from that string that are a part of a secondary user inputted string', that should be a piece of cake. In a few days or so, might submit such an example, based predominately upon the 'remove vowels' example.
- Steve's blog
- 1 comment
- 170 reads
- Email this page
Calculate Pace (Running Program) - # 2
Submitted by Steve on 23 February, 2008 - 11:29.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?
- Steve's blog
- 1 comment
- 228 reads
- Email this page
Array Solution - Half and half, 10 per line
Submitted by Steve on 30 December, 2007 - 00:40.Problem : Declare and initialize an integer array having 50 elements. The value of the first 25 elements to be the square of the index element and the remaining 25 elements, the values to be treble the index element. Further, the results are to be printed 10 per line.
This was a problem put forward from this thread.
- Steve's blog
- 4 comments
- Read more
- 517 reads
- Email this page
Remove Vowels
Submitted by Steve on 29 December, 2007 - 11:34.Perusing the forums I came across this thread a question relating to inputting a string and then redisplaying the string without the vowels. What I failed to notice was that the thread was a C thread (not C++) and so my C++ solution, not precisely addressing the original posters question. This, not such a problem for me, as it helped cement my learning to date.
- Steve's blog
- 2 comments
- Read more
- 653 reads
- Email this page
Calculate Pace (Running Program)
Submitted by Steve on 23 December, 2007 - 21:39.During week 5 of study, learning functions and in particular references passed to and from functions. The following program, in a way, cemented some of the stuff studied. It wasn't a part of the book but since I'm interested in running, I thought I'd write a little program that calculates the average pace of a run given the distance (km) and duration (time) of the run.
- 1 comment
- Read more
- 352 reads
- Email this page


Recent comments
2 days 1 hour ago
1 week 3 days ago
2 weeks 15 hours ago
2 weeks 20 hours ago
2 weeks 1 day ago
5 weeks 17 hours ago
6 weeks 5 days ago
6 weeks 5 days ago
7 weeks 17 hours ago
7 weeks 1 day ago