Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need some help completing this program, I have the format set up already (below the prompt) and also output of the program. For this

I need some help completing this program, I have the format set up already (below the prompt) and also output of the program.

For this program declare a structure called Time that contains members to store the hours, minutes and seconds. Prompt the user for the input time and then call the GetTime function, which is responsible for getting three integer values from stdin, each which represent the hours, minutes and seconds. The GetTime function should test to see if each value is successfully extracted, and if it is, to store it in the corresponding member of the Time parameter. If either the extraction fails or a particular value is out of range, then an appropriate error message is written to stdout, and the function returns a value of false. Otherwise, if all went well, a value of true is returned to the caller. (Note: this program assumes a zero-based 24-hour time, so the range of valid values for the hours is from 0 to 23, and for minutes and seconds, is 0 to 59.)

If GetTime returns a value of true to the main function, then main will proceed to display the entered time in HH:MM:SS format, by calling the DispTime function. After the time has been displayed, main will then ask the user how many times they would like to see the time incremented. The user will enter an integer value in response, and a loop is then entered where the AddOneSecond function is called to add a second to the time, then DispTime will be called to display the incremented time. This happens as many times as the user wants to increment the time. To add a bit of realism to the whole thing, on each loop iteration a call is made to the sleep function, which causes the loop to suspend program execution for one second before continuing with the next loop iteration. This way it'll look more like a clock is running! ( sleep function is available with #include , and it takes a single integer argument that represents the number of seconds to sleep.)

Complete the implementation of three functions: GetTime, AddOneSecond and DispTime. also declare the Time structure!

#include

#include

#include

#include

using namespace std;

// the Time structure declaration

? ? ?

// defined constants

const int MAX_HOURS = 23;

const int MAX_MINS = 59;

const int MAX_SECS = 59;

// function prototypes

void AddOneSecond(Time &timeRef);

bool GetTime(Time &timeRef);

void DispTime(const Time &timeRef);

// ==== main ==================================================================

//

// ============================================================================

int main()

{

Time userTime;

int numSeconds;

// get the time from the user

cout

if (false == GetTime(userTime))

{

// call the 'exit' function to terminate the program

exit(EXIT_FAILURE);

}

// get the number of times to increment from the user

cout

DispTime(userTime);

cout

cin >> numSeconds;

// loop and show the incremented time

for (; numSeconds > 0; --numSeconds)

{

AddOneSecond(userTime);

DispTime(userTime);

sleep(1);

}

return 0;

} // end of main

// ==== AddOneSecond ==========================================================

//

// This function examines the input time parameter and adds one second to it.

// If any of the fields (i.e., the seconds, minutes or hours) need to be

// reset to zero as a result of adding the second, it is handled by this

// function.

//

// Input:

// timeRef [IN/OUT] -- a reference to a Time structure

//

// Output:

Nothing; the caller's Time structure will be updated.

//

// ============================================================================

void AddOneSecond(Time &timeRef)

{

? ? ?

} // end of "AddOneSecond"

// ==== DispTime ==============================================================

//

// This function uses the input Time parameter to display a formatted time to

// stdout. The format used is HH:MM:SS, where the hours, minutes and seconds

// displayed in field widths of two characters with leading zeros if necessary.

//

// Input:

// timeRef [IN] -- a reference to a Time structure

//

// Output:

// Nothing.

//

// ============================================================================

void DispTime(const Time &timeRef)

{

? ? ?

} // end of "DispTime"

// ==== GetTime ===============================================================

//

// This function extracts the hours, minutes and seconds integer values from

// stdin and stores them in the reference parameter. As an attempt is made to

// read a value, it is tested for successful extraction, and also tested to

// make sure it is within the appropriate range for that value (e.g., the hours

// must be between zero and 23, the minutes and seconds between zero and 59).

// If either an extraction fails or the extracted value is out of range, an

// error message is written to stdout and a value of false is returned. If all

// values are successfully extracted and within range, a value of true is

// returned.

//

// Input:

// timeRef [OUT] -- a reference to a Time structure

//

// Output:

// A value of true if all integer values are successfully extracted and

// within appropriate range; also, the caller's argument will contain the

// extracted values. Else, if a failed extraction is encountered or a

// value is extracted, a value of false is returned.

//

// ============================================================================

bool GetTime(Time &timeRef)

{

? ? ?

} // end of "GetTime"

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

include iostream> #include iomanip> std #include using namespace /I the Time structure declaration /I defined constants const int MAX_HOURS 23; const int MAX MINS = 59; const int MAX SECS = 59; void Addonesecond(Time &timeRef) bool GetTime(Time &timeRef) void DispTime(const Time &timeRef) main ?int main( Time int userTime; numSeconds // get the time from the user cout ? "Enter the hours, minutes and seconds values separated ?? "with (false "; spaces: GetTime(usertime)) if /I call the 'exit function to terminate the program exit (EXIT_FAILURE); // get the number of times to increment from the user cout ?? "The time you entered is "; DispTime(userTime); cout 8; --numSeconds) Addonesecond userTime); DispTime(userTime)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions