Page 2: Page 3: struct Time - Write a program that prompts the user for the hour
ID: 3649303 • Letter: P
Question
Page 2:
Page 3:
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 is output to the screen The program should include: struct Time Declare a struct named: Time The struct has 3 data members: hours, minutes, and seconds (all integers) In main(), declare an object of Time and name it: time Four functions in the program getTime - A void-returning function, gets input from user The object, time, is passed by reference to the function. Note: Objects of structs and classes are always passed by reference, The function prompts the user to enter the time. The function reads the time entered by the user The function returns no value. IsTimeValid - Function returns a bool data type (true or false), The function Is called from the getTime function. The parameter for this function is the time object that holds the hours, minutes, and seconds representing the time entered by the user This function checks to make sure the time entered is valid. HINT: if ((time.hours >=0) && (time.hours 59; MAX_SECS = 59; If the function returns true, the getTime function is done and program execution returns to main(). However, if the function returns false, then a message displays: Invalid time Still within the getTime function, the user Is again prompted for the time. Again the time is read and passed to the isTimeValid function, and so on. Therefore, a do-while (or while) loop must bo Included within the getTime function. Within the loop, the user Is prompted for a time, and the isTimeValid function is called. addOneSecond - A void-returning function adds one second to the time entered displayTime - A void-returning function displays the time after the second has been added. cout.fill('0') - This library function will fill any empty spaces with the specified character It checks to see what setw is, and then fills if needed. Include a do/while loop, along with system("cls") - (Allows user to repeat program) OUTPUT #1 Enter the time in "military time", (24-hour format), in the following order: HH:MM:SS, (Hours, Minutes, Seconds) Hours: 14 Minutes: 44 Seconds: 22 After adding one second, the time is 14 44:23 Do it again? (Y/N) y OUTPUT #2 Enter the time in "military time", (24-hour format), in the following order: HH.MM.SS, (Hours, Minutes, Seconds). Hours: 1 Minutes: 9 Seconds: 59 After adding one second, the time is 01:10:00. Do it again? (Y/N) y OUTPUT #3 Enter the time in "military time",24-hour format), in the following order: HH:MM:SS (Hours, Minutes, Seconds). Hours: 21 Minutes: 59 Seconds: 59 After adding one second, the time is 22: 00: 00. Do it again? (Y/N) y OUTPUT #4 Enter the time in "military time", (24-hour format), in the following order: HH:MM;SS, (Hours, Minutes, Seconds). Hours: 23 Minutes: 59 Seconds: 59 After adding one second, the time is 00:00.-00. Do it again? (Y/N) y OUTPUT #5 Enter the time in "military time", (24-hour format), in the following order: HH:MM:SS, (Hours, Minutes, Seconds). Hours. 27 Minutes. 23 Seconds: 11 Invalid data. Enter the time in "military time", (24-hour format), in the following order: HH:MM SS, (Hours, Minutes, Seconds).Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
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&);
bool isTimeValid(Time);
void addOneSecond(Time&);
void displayTime(Time);
int main()
{Time time;
char again;
do
{system("cls");
getTime(time);
addOneSecond(time);
displayTime(time);
cout<<"Do it again? (Y/N) ";
cin>>again;
}while(again=='Y'||again=='y');
return 0;
}
void getTime(Time& time)
{
cout<<"Enter the time in "military time",(24 hour format), ";
cout<<"in the following order: HH:MM:SS:, (Hours, Minutes, Seconds) ";
cout<<"Hours: ";
cin>>time.hours;
cout<<"Minutes: ";
cin>>time.minutes;
cout<<"Seconds: ";
cin>>time.seconds;
while(!isTimeValid(time))
{cout<<"Invalid time ";
cout<<"Enter the time in "military time",(24 hour format), ";
cout<<"in the following order: HH:MM:SS:, (Hours, Minutes, Seconds) ";
cout<<"Hours: ";
cin>>time.hours;
cout<<"Minutes: ";
cin>>time.minutes;
cout<<"Seconds: ";
cin>>time.seconds;
}
}
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;
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<<"After adding one second, the time is ";
cout<<setw(2)<<time.hours<<":"<<setw(2)<<time.minutes<<":"<<setw(2)<<time.seconds<<" ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.