Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Output
- A military time of 0012:3 = 12:12:3
- A military time of 1145:5 = 11:45:5
- A military time of 1419:34 = 2:19:34
- Enter a number of military hours in the format #### followed by a space and then
- a number of seconds: 2401
- 62
- Sorry, that wasn't a valid time. Try again.
- Enter a number of military hours in the format #### followed by a space and then
- a number of seconds:
- //Source code
- //Christopher Collins
- //Comp sci 2
- //4/25/19
- //MilTime.cpp
- //MilTime.cpp holds the implementation of methods from the MilTime.h file
- //MilTime.cpp holds the implementation of methods from the MilTime.h file
- #include "MilTime.h"
- using namespace std;
- void MilTime::setTimeVars()
- {
- //set Time min and sec directly
- sec = milSeconds;
- min = stoi(milHours.substr(2, 2)); //pull off the last two digits of the military hour
- int tempH = stoi(milHours.substr(0, 2)); //pull off the first two digits
- if (tempH == 0) //handle case of midnight hour
- hour = 12;
- else if (tempH >= 1 && tempH <= 12)
- hour = tempH; //no conversion needed
- else
- //subtract 12 from larger num to get regular hour
- hour = tempH - 12;
- }
- bool MilTime::setTime(string milh, int mils)
- {
- bool retVal = false;
- if (stoi(milh) >= 0 && stoi(milh) <= 2359 && mils >= 0 && mils <= 59)
- {
- milHours = milh;
- milSeconds = mils;
- setTimeVars();
- retVal = true;
- }
- return retVal;
- }
- //Time.h
- #pragma once
- //Time.h file from page 972 in the 9/e of the book
- class Time
- {
- protected:
- int hour;
- int min;
- int sec;
- public:
- Time()
- {
- hour = 0; min = 0; sec = 0;
- }
- Time(int h, int m, int s)
- {
- hour = h; min = m; sec = s;
- }
- int getHour() const
- {
- return hour;
- }
- int getMin() const
- {
- return min;
- }
- int getSec() const
- {
- return sec;
- }
- };
- #pragma once
- //MilTime.h
- //holds military time in hours and seconds (i.e. 1430 with 3 seconds)
- //inherits from Time class provided
- #include "Time.h"
- #include <cstdlib>
- #include <string>
- #include<iostream>
- using namespace std;
- class MilTime : public Time
- {
- private:
- string milHours;
- int milSeconds;
- void setTimeVars();
- public:
- MilTime() : MilTime("0000", 0)
- {}
- MilTime(string milh, int mils)
- {
- setTime(milh, mils);
- }
- bool setTime(string, int);
- string getHour() const
- {
- return milHours;
- }
- int getStandHr() const
- {
- return hour;
- }
- };
- class BadHours :public exception {
- public:BadHours() {
- }
- const char* x() const throw()
- {
- return "Invalid number for military time hours\n";
- }
- };
- class BadSeconds :public exception {
- public:BadSeconds() {
- }
- const char* x() const throw()
- {
- return "Invalid number for military time seconds\n";
- }
- };
- //Main.cpp
- #include "Miltime.h"
- #include "Time.h"
- #include <iostream>
- #include <string>
- #include "main.h"
- using namespace std;
- int main()
- {
- MilTime m1("0012", 3);
- int mSeconds;
- string mHours;
- int converter;
- cout << "A military time of ";
- cout << m1.getHour() << ":";
- cout << m1.getSec() << " = ";
- cout << m1.getStandHr();
- cout << ":" << m1.getMin() << ":";
- cout << m1.getSec() << endl;
- m1.setTime("1145", 5);
- cout << "A military time of ";
- cout << m1.getHour() << ":";
- cout << m1.getSec() << " = ";
- cout << m1.getStandHr() << ":";
- cout << m1.getMin() << ":";
- cout << m1.getSec() << endl;
- m1.setTime("1419", 34);
- cout << "A military time of " << m1.getHour();
- cout << ":" << m1.getSec();
- cout << " = " << m1.getStandHr() << ":";
- cout << m1.getMin() << ":";
- cout << m1.getSec() << endl;
- cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
- cin >> mHours;
- converter = stoi(mHours);
- cin >> mSeconds;
- try {
- if (converter > 0000 && converter <= 2359)//This is the input validation for the string it converts mhours from a string to an int
- {//And it does this inorder for it to make the input validation for greater than less than easier
- if (mSeconds > 00 && mSeconds < 58)
- {
- cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
- cin >> mHours >> mSeconds;
- cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
- }
- else
- {
- BadSeconds sec;
- throw sec;
- }
- }
- else
- {
- BadHours hr;
- throw hr;
- }
- }
- catch (BadHours hr)
- {
- cout << hr.x();
- }
- catch (BadSeconds sec)
- {
- cout << sec.x();
- }
- system("pause");
- }
- {
- throw "Improper output exception 36";
- }
- }
- try
- {
- cin >> mSeconds;
- }
- catch (const std::exception&)
- {
- if (mSeconds >= 61 || mSeconds <= -1)
- {
- throw "Improper output exception 36";
- }
- }
- isValid = m1.setTime(mHours, mSeconds);
- while (!isValid)
- {
- cout << "Sorry, that wasn't a valid time. Try again.";
- cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
- cin >> mHours >> mSeconds;
- isValid = m1.setTime(mHours, mSeconds);
- }
- cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement