215 lines
4.5 KiB
C++
215 lines
4.5 KiB
C++
/*
|
|
* Clock app for Project One
|
|
* Clock.h header file
|
|
*
|
|
* Date: 2022/03/16
|
|
* Author: Cody Cook
|
|
*/
|
|
|
|
#pragma once // only read this file once
|
|
#include "Main.h" // grab clearScreen function
|
|
#include <iostream> // use of cin, cout
|
|
#include <string> // use of string
|
|
#include <cstring> // use of string functions
|
|
|
|
using namespace std;
|
|
|
|
unsigned int hour; // the clock's hour
|
|
unsigned int minute; // the clock's minute
|
|
unsigned int second; // the clock's second
|
|
|
|
string twoDigitString(unsigned int n)
|
|
{
|
|
/// Convert an unsigned integer to a two character string equivalent.
|
|
|
|
// the result of the conversion from int to string
|
|
string value;
|
|
|
|
// when n is less than 10, add a leading zero
|
|
if (n < 10 && n >= 0)
|
|
{
|
|
value = "0" + to_string(n);
|
|
}
|
|
else if (n >= 10 && n < 60)
|
|
{
|
|
value = to_string(n);
|
|
}
|
|
else
|
|
{
|
|
value = "00";
|
|
}
|
|
|
|
// return the new string
|
|
return value;
|
|
}
|
|
|
|
string nCharString(unsigned int n, char c)
|
|
{
|
|
/// return the c character n times.
|
|
return string(n, c);
|
|
}
|
|
|
|
string formatTime24(unsigned int h, unsigned int m, unsigned int s)
|
|
{
|
|
/// return the concatenation
|
|
return twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s);
|
|
}
|
|
|
|
string formatTime12(unsigned int h, unsigned int m, unsigned int s)
|
|
{
|
|
/// set A M to default and change to P M when it is appropriate
|
|
string ampm = "A M";
|
|
if (h >= 12)
|
|
{
|
|
h -= 12;
|
|
ampm = "P M";
|
|
}
|
|
if (h == 0)
|
|
{
|
|
h = 12;
|
|
}
|
|
|
|
/// return the concat of thes values
|
|
return twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s) + " " + ampm;
|
|
}
|
|
|
|
void displayClocks(unsigned int h, unsigned int m, unsigned int s)
|
|
{
|
|
/// display the clocks in a table
|
|
|
|
// clear the screen
|
|
clearScreen();
|
|
|
|
// output the table, 12 hour on left, 24 hour on right.
|
|
cout << nCharString(27, '*') << nCharString(3, ' ') << nCharString(27, '*') << endl;
|
|
cout << "*" << nCharString(6, ' ') << "12-Hour Clock" << nCharString(6, ' ') << "*" << nCharString(3, ' ');
|
|
cout << "*" << nCharString(6, ' ') << "24-Hour Clock" << nCharString(6, ' ') << "*" << endl;
|
|
cout << endl;
|
|
cout << "*" << nCharString(6, ' ') << formatTime12(h, m, s) << nCharString(7, ' ') << "*" << nCharString(3, ' ');
|
|
cout << "*" << nCharString(8, ' ') << formatTime24(h, m, s) << nCharString(9, ' ') << "*" << endl;
|
|
cout << nCharString(27, '*') << nCharString(3, ' ') << nCharString(27, '*') << endl;
|
|
}
|
|
|
|
unsigned int getHour()
|
|
{
|
|
/// return the hour value
|
|
return hour;
|
|
}
|
|
void setHour(unsigned int h)
|
|
{
|
|
/// set the hour value
|
|
hour = h;
|
|
return;
|
|
}
|
|
void addOneHour()
|
|
{
|
|
/// add one hour to the clock; if the hour is 23, set it to 0
|
|
if (getHour() >= 0 && getHour() <= 22)
|
|
{
|
|
setHour(getHour() + 1);
|
|
}
|
|
else if (getHour() == 23)
|
|
{
|
|
setHour(0);
|
|
}
|
|
return;
|
|
}
|
|
unsigned int getMinute()
|
|
{
|
|
/// return the minute value
|
|
return minute;
|
|
}
|
|
void setMinute(unsigned int m)
|
|
{
|
|
/// set the minute value
|
|
minute = m;
|
|
return;
|
|
}
|
|
void addOneMinute()
|
|
{
|
|
/// add one minute to the clock; if the minute is 59, add one hour
|
|
if (getMinute() >= 0 && getMinute() <= 58)
|
|
{
|
|
setMinute(getMinute() + 1);
|
|
}
|
|
else if (getMinute() == 59)
|
|
{
|
|
setMinute(0);
|
|
addOneHour();
|
|
}
|
|
return;
|
|
}
|
|
|
|
unsigned int getSecond()
|
|
{
|
|
/// return the second value
|
|
return second;
|
|
}
|
|
|
|
void setSecond(unsigned int s)
|
|
{
|
|
/// set the second value
|
|
second = s;
|
|
return;
|
|
}
|
|
|
|
void addOneSecond()
|
|
{
|
|
/// add one second to the clock; if the second is 59, add one minute
|
|
if (getSecond() >= 0 && getSecond() <= 58)
|
|
{
|
|
setSecond(getSecond() + 1);
|
|
}
|
|
else if (getSecond() == 59)
|
|
{
|
|
setSecond(0);
|
|
addOneMinute();
|
|
}
|
|
return;
|
|
}
|
|
|
|
void setTime()
|
|
{
|
|
/// Allow the user to set the time. Required 24-hour time format.
|
|
|
|
int userHour, userMinute, userSecond = -1; // use regular integers to handle exception
|
|
bool validTime = false; // flag to determine if the time is valid
|
|
|
|
while (!validTime)
|
|
{
|
|
// ask user for the current time; then parse and configure.
|
|
cout << "Please enter the current time in 24-hour format (HH:MM:SS): ";
|
|
|
|
// set the hour and ignore the :
|
|
cin >> userHour;
|
|
cin.ignore();
|
|
|
|
// set the minutes and ignore the :
|
|
cin >> userMinute;
|
|
cin.ignore();
|
|
|
|
// set the seconds
|
|
cin >> userSecond;
|
|
cin.ignore();
|
|
|
|
// validate the input
|
|
if ((userHour >= 0 && userHour <= 23) && (userMinute >= 0 && userMinute <= 59) && (userSecond >= 0 && userSecond <= 59))
|
|
{
|
|
validTime = true;
|
|
}
|
|
else
|
|
{
|
|
clearCin();
|
|
userHour, userMinute, userSecond = -1;
|
|
cout << "Invalid time entered. Please try again." << endl;
|
|
}
|
|
}
|
|
|
|
setHour(userHour);
|
|
setMinute(userMinute);
|
|
setSecond(userSecond);
|
|
|
|
// now that time is set, clear the screen and show the clocks
|
|
clearScreen();
|
|
displayClocks(hour, minute, second);
|
|
} |