C++ struct time write a program that prompts the user for the hour, minutes, and
ID: 3590551 • Letter: C
Question
C++ struct time write a program that prompts the user for the hour, minutes, and seconds for a 24-hour time(military time). If the time entered is valid, then one second is added to it, and the new time it output to the screen. struct Time Declare a srtuct named: Time, The struct has 3 data members: hours, minutes, seconds (all interger). In main0 declare an object of Time and name it time - Four function . getTime A void retuning fumction, gets input from user. The object, time, is passed by reference to the function. Note Objects of struct and classes are always passed by refence. The fumction prompts the user to enter the ime. The function reads the time entered by the user. The function returns no value 2. isTimeValid Function returns a bool date type (true or false). The function is called from the getTime finction. The parameter for this function is the time onject that holds the hous, minutes, seconds representing the time entered by the user. This function checks make sure the time entered is valid. HINTif time hours0) && (time hours MAX SECS) { timeseconds = 0: time minutes++, if {"" 4. displayTime: A void returning function displays the time after the second has been added. cout fil(0) The library function wil any empty spaces with the secified character. It checks to see whatt setw is, and the fills if needed. EX: coutExplanation / Answer
Given below is the code with output. Please don't forget to rate the answer if it helped. thank you.
#include <iostream>
#include <iomanip>
using namespace std;
//global constants
const int MAX_HOURS = 23;
const int MAX_MINS = 59;
const int MAX_SECS = 59;
struct Time
{
int hours;
int minutes;
int seconds;
};
void getTime(Time &time);
bool isTimeValid(Time time);
void addOneSecond(Time &time);
void displayTime(Time time);
int main()
{
Time time;
string ans = "y";
do
{
getTime(time);
addOneSecond(time);
cout << "After adding one second, the time is ";
displayTime(time);
cout << "Do it again? Y/N: ";
cin >> ans;
}while(ans == "y" || ans == "Y");
}
void getTime(Time &time)
{
bool valid = true;
do
{
cout << "Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS" << endl;
cout << "Hours: ";
cin >> time.hours;
cout << "Minutes: ";
cin >> time.minutes;
cout << "Seconds: ";
cin >> time.seconds;
valid = isTimeValid(time);
if(!valid)
cout << "Invalid Time !" << endl;
}while(!valid);
}
bool isTimeValid( Time time)
{
if(time.hours >= 0 && time.hours <= MAX_HOURS &&
time.minutes >= 0 && time.minutes <= MAX_MINS &&
time.seconds >= 0 && time.seconds <= MAX_SECS)
return true;
else
return false;
}
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;
}
}
}
void displayTime(Time time)
{
cout.fill('0');
cout << setw(2) << time.hours <<
":" << setw(2) << time.minutes << ":" << setw(2) << time.seconds << endl;
}
output
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 14
Minutes: 44
Seconds: 22
After adding one second, the time is 14:44:23
Do it again? Y/N: y
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 1
Minutes: 9
Seconds: 59
After adding one second, the time is 01:10:00
Do it again? Y/N: y
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 21
Minutes: 59
Seconds: 59
After adding one second, the time is 22:00:00
Do it again? Y/N: y
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 23
Minutes: 59
Seconds: 59
After adding one second, the time is 00:00:00
Do it again? Y/N: y
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 27
Minutes: 23
Seconds: 11
Invalid Time !
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 6
Minutes: 58
Seconds: 69
Invalid Time !
Enter the time in 'military time', (24-hour format), in the following order- HH:MM:SS
Hours: 6
Minutes: 58
Seconds: 59
After adding one second, the time is 06:59:00
Do it again? Y/N: n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.