// Attached: HW_3f
// ===========================================================
// File: HW_3f.cpp
// ===========================================================
// Programmer: Elaine Torrez
// Class: CMPR 121
// ===========================================================
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
// ===========================================================
// Global Constants
// ===========================================================
const int MAX_HOURS = 23;
const int MAX_MINS = 59;
const int MAX_SECS = 59;
// ===========================================================
// Struct
// ===========================================================
struct Time
{
int hours;
int minutes;
int seconds;
};
// Function Prototypes
void getTime(Time &time);
bool isTimeValid(const Time &time);
void addOneSecond(Time &time);
void displayTime(const Time &time);
// ==== main ====================================================
// Prompts user for a valid 24-hour time, adds one second,
// displays the result, and allows repetition.
// =============================================================
int main()
{
Time time;
char again = 'n';
do
{
getTime(time);
addOneSecond(time);
displayTime(time);
cout << "Do it again? (Y/N) ";
cin >> again;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (again == 'Y' || again == 'y');
return 0;
} // end of main()
// =============================================================
// ==== getTime =================================================
// Gets hours, minutes, and seconds from the user.
// Repeats until valid time is entered.
//
// Input:
// time - (IN & OUT) Time struct passed by reference
//
// Output:
// None
// =============================================================
void getTime(Time &time)
{
bool validInput = false;
do
{
cout << "Enter the time in \"military time\", (24-hour format),\n";
cout << "in the following order: HH:MM:SS, (Hours, Minutes, Seconds).\n";
cout << "Hours: ";
if (!(cin >> time.hours))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid data.\n\n";
continue;
}
cout << "Minutes: ";
if (!(cin >> time.minutes))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid data.\n\n";
continue;
}
cout << "Seconds: ";
if (!(cin >> time.seconds))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid data.\n\n";
continue;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
validInput = isTimeValid(time);
if (!validInput)
{
cout << "Invalid data.\n\n";
}
} while (validInput != true);
} // end of getTime()
// =============================================================
// ==== isTimeValid ============================================
// Checks if the entered time is valid (24-hour format).
//
// Input:
// time - (IN) Time struct containing hours, minutes, seconds
//
// Output:
// Returns true if valid, false otherwise
// =============================================================
bool isTimeValid(const Time &time)
{
if (time.hours < 0 || time.hours > MAX_HOURS)
return false;
if (time.minutes < 0 || time.minutes > MAX_MINS)
return false;
if (time.seconds < 0 || time.seconds > MAX_SECS)
return false;
return true;
} // end of isTimeValid()
// =============================================================
// ==== addOneSecond ===========================================
// Adds one second to the given time and handles rollovers.
//
// Input:
// time - (IN & OUT) Time struct passed by reference
//
// Output:
// None
// =============================================================
void addOneSecond(Time &time)
{
time.seconds++;
if (time.seconds > MAX_SECS)
{
time.seconds = 0;
time.minutes++;
if (time.minutes > MAX_MINS)
{
time.minutes = 0;
time.hours++;
if (time.hours > MAX_HOURS)
{
time.hours = 0;
}
}
}
} // end of addOneSecond()
// =============================================================
// ==== displayTime ============================================
// Displays the time after adding one second in HH:MM:SS format.
//
// Input:
// time - (IN) Time struct
//
// Output:
// Prints formatted time
// =============================================================
void displayTime(const Time &time)
{
cout << "After adding one second, the time is ";
cout.fill('0');
cout << setw(2) << time.hours << ":"
<< setw(2) << time.minutes << ":"
<< setw(2) << time.seconds << ".\n";
cout.fill(' ');
} // end of displayTime()
// =============================================================