Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW 3a struct Time Write a program that prompts the user for the hour, minutes, a

ID: 3910288 • Letter: H

Question

HW 3a 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 in, 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) o In main), declare an object of Time and name it: Four funstions in the program: 1.) getTime-A y?d-returning function, gets input from user time - The object, time, is passed by reference to the function. o Note: Objects of structs and classes are always passed by reference. o The function prompts the user to enter the time o The function reads the time entered by the user o The function returns no value. 2.) 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 o and seconds representing the time entered by the user. This function checks to make sure the time entered is valid. o HINT: if (time.hours >-o) && (time.hoursMAX HOURS)) &8... (if the time entered is invalid, return false, else return true) Include these global constants: MAX HOURS-23; MAX MINS 59; MAX-SECS = 59; If the function retums true, the getTime function is done and program execution o returns to main). However, if the function returns false, then a message displays: Invalid time o o o Still within the getTime function, the user is again prompted for the time. Again the time is read and passed to the is Time Valid function, and so on. o Therefore, a do-while (or while) loop must be included within the getTime function. Within the loop, the user is prompted for a time, and the is TimeValid function is called

Explanation / Answer

Hello there, I have written complete C++ code as per your requirements:

Source code==>

#include<iostream>
#include<stdlib.h>
#include <iomanip>
#define MAX_HOURS 23
#define MAX_MINS 59
#define MAX_SECS 59
using namespace std;

typedef struct time
{
   int hours;
   int minutes;
   int seconds;
}Time;

bool isTimeValid(Time& t)
{
   if(((t.hours >=0) && (t.hours <= MAX_HOURS)) && ((t.minutes >=0) && (t.hours <= MAX_MINS)) && ((t.seconds >=0) && (t.seconds <= MAX_SECS)))
   {
       return 1;
   }
   return 0;
}

void addOneSecond(Time& t)
{
   ++t.seconds;
   if(t.seconds > MAX_SECS)
   {
       t.seconds = 0;
       ++t.minutes;
       if(t.minutes > MAX_MINS)
       {
           t.minutes = 0;
           ++t.hours;
           if(t.hours > MAX_HOURS)
           {
               t.hours = 0;
           }
       }
   }
}

void getTime(Time& t)
{
   do{

             cout<< "Enter the time in "military time ",in following order: HH:MM:SS (Hours, Minutes, Seconds) ";
             cout << "Hours: ";    cin >> t.hours;
             cout << "Minutes: "; cin >> t.minutes;
             cout << "Seconds: "; cin >> t.seconds;
         if(!isTimeValid(t))
         {
             cout << "Invalid Data. " << endl;
         }
   }while(!isTimeValid(t));

}

void displayTime(Time& t)
{
   cout << setw(2);
   cout.fill('0');
   cout << t.hours << ":";
   cout << setw(2);
   cout.fill('0');
   cout << t.minutes <<":";
   cout << setw(2);
   cout.fill('0');
   cout << t.seconds << endl;
}

int main()
{
   Time T;
   char ch;
   getTime(T);
        do
   {
      addOneSecond(T);
      cout << "After adding one second, the time is ";
          displayTime(T);
      cout << "Do it again? (Y/N)";  
      cin >> ch;
      system("clear");
   }while(ch != 'N' && ch !='n');

   return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I have executed it on Linux so I used system("clear"), you can change it according to your operating system.

Feel free to ask qeries regarding above solution, I am always here to help you!!!

Thank you!!!