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

(Exercise 1) Create -timelnput.cpp In this part of the lab, you will be creating

ID: 3673752 • Letter: #

Question



(Exercise 1) Create -timelnput.cpp In this part of the lab, you will be creating your own Time structure to keep track of various times of the day. Your Time structure should contain 3 integer variables: . one for the hour . one for the minutes . one for the seconds Your program will setup a start & end time for a lecture at the University. It will ask the user to enter the start time and the end time for the lecture, using the 24 hour format (HH:MM:SS). You need to check that the input produces a valid time: check if the string contains the right characters (i.e. two characters [0-9], a colon, etc.) check if the time entered makes sense (i.e. HR is 0-23, etc.... for instance, 33:79:99 does NOT make sense) If the time is incorrect, output an error message and exit the program Once the user has entered two valid times, your program will output the start and end times in a 24 hour format. For those who are unfamiliar with 24 hour format, please see http://en.wikipedia.org/wiki/12-hour_clock Note: in order to receive full credit for this part of the lab, you MUST create a main program that defined the Time structure and the following two functions: getTimeFromUser: takes in a Time structure variable as a parameter, reads a string from user, checks to see if it is a valid 24 hour format, stores it inside the Time structure variable, and return true or false depending on whether or not the time is valid. Since the Time structure is no need to pass the reference (&) of the Time structure instead of the identifier (name) as the parameter. This way, the contents of the Time structure will be updated after the function is executed and goes back to the main function. t returned at the end of this function, you Write a pseudocode of getTimeFromUser function before writing it in C++ code o .print24Hour: takes in a Time structure variable as a parameter and prints the time using the 24 hour format.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

struct TIME{

int seconds;

int minutes;

int hours;

};

boolean getTimeFromUser(*s)

{

cin.ignore();

getline(cin,s);

//12:04:59

hours= s.substr(1,2);

//starting index=1 and how many characters we want is 2 i.e 12

minutes= s.substr(4,2);//i.e 04

seconds= s.substr(7,2);//i.e 59

//checking the time is valid or not ,not valid return false

if(hours<<0 && hours>>12)

{

if(mintutes<<0&&mintutes>>60)

{

if(seconds<<0 && seconds>>60)

{

return false;

}

}

}

return true;

}

void Print24Hour(struct *t)

{

printf("= %d:%d:%d ",t.hours,t.minutes,t.seconds);

}

void difference(struct TIME t1, struct TIME t2, struct TIME *differ)

{

    if(t2.seconds>t1.seconds){

        --t1.minutes;

        t1.seconds+=60;

    }

    differ->seconds=t1.seconds-t2.seconds;

    if(t2.minutes>t1.minutes){

        --t1.hours;

        t1.minutes+=60;

    }

    differ->minutes=t1.minutes-t2.minutes;

    differ->hours=t1.hours-t2.hours;

}

int main(){

char s[8];

struct TIME t1,t2,diff;

printf("Enter starting time of a lecture(format is HH:MM:SS) ");

getTimeFromUser(&t1);//getting starting time

if(getTimeFromUser(&t1)==false)

printf("The start time entered invaild");

printf("Enter ending time of a lecture(format is HH:MM:SS) ");

getTimeFromUser(&t2);//getting end time

if(getTimeFromUser(&t2)==false)

printf("The end time entered invaild");

difference(t1,t2,&diff);

   printf(" The lecture starts at %c and ending time is %c ", Print24Hour(&t1), Print24Hour(&t2));

    return 0;

}