adspace
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
Answer Posted / 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 View All Answers