Write a program which is required to process the time of a
clock in hours and minutes, entered from the keyboard. With
this program, there are two requirements for any data
entered by a user:
1. The data must be of the correct type (in this case, two
ints).
2. The data must be in the correct range: this means that,
for the minutes, negative numbers and any number above 59
must be rejected; for the hours, negative numbers and any
number above 23 must be rejected.
Output error message for invalid data input.
Output the time one and a half hour after the time input.
i.e.
Hour: 22
Min: 32
One and a half hour after 22:32 is 00:02



Write a program which is required to process the time of a clock in hours and minutes, entered fro..

Answer / Nishu Sharma

Here's a C++ program that processes clock time in hours and minutes, with validations and calculations:

```cpp
#include <iostream>
#include <cmath>

int main() {
int hours = 0;
int minutes = 0;

std::cout << "Enter the hour (1-23): ";
std::cin >> hours;
if (hours < 1 || hours > 23) {
std::cerr << "Invalid hour value, please enter a number between 1 and 23.n";
return 1;
}

std::cout << "Enter the minute (0-59): ";
std::cin >> minutes;
if (minutes < 0 || minutes > 59) {
std::cerr << "Invalid minute value, please enter a number between 0 and 59.n";
return 1;
}

int totalMinutes = hours * 60 + minutes;
int oneHalfHours = totalMinutes + (30 * 60);

int newHours = oneHalfHours / 60;
int newMinutes = oneHalfHours % 60;

if (newHours > 23) {
newHours -= 24;
}

std::cout << "One and a half hour after " << hours << ":" << minutes << " is ";
std::cout << newHours << ":";
if (newMinutes < 10) {
std::cout << "0";
}
std::cout << newMinutes << "n";

return 0;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

How can you create a virtual copy constructor?

1 Answers  


In inline " expression passed as argument are evalauated once " while in macro "in some cases expression passed as argument are evaluated more than once " --> i am not getting it plz help to make me understand....

1 Answers   College School Exams Tests, CS,


Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?

1 Answers  


Explain the difference between struct and class in terms of access modifier.

1 Answers  


What are keywords in c++?

1 Answers  


Are vectors faster than arrays?

1 Answers  


Brief explaination about #include<iostream.h>, cin and cout

3 Answers  


What is the difference between the compiler and the preprocessor?

1 Answers  


Difference between a homogeneous and a heterogeneous container

1 Answers  


What does ctime() do?

1 Answers  


what are Access specifiers in C++ class? What are the types?

1 Answers  


What number of digits that can be accuratly stored in a float (based on the IEEE Standard 754)? a) 6 b) 38 c) An unlimited number

1 Answers  


Categories