5
5
#include < iostream>
6
6
using namespace std ;
7
7
8
- double converting1 (double hours, double minutes, double seconds)
9
- { // initializing the variables
10
- return (hours * 60 ) + (minutes) + (seconds / 60 ); // returning the function "converting"
8
+ bool GetMinutesFromTime (double hours, double minutes, double seconds) {
9
+
10
+ if (hours < 0 ) {
11
+ cout << " Your hours input is wrong" << endl;
12
+ return false ;
13
+ }
14
+ if (minutes < 0 ) {
15
+ cout << " Your minutes input is wrong" << endl;
16
+ return false ;
17
+ }
18
+ if (seconds < 0 ) {
19
+ cout << " Your seconds input is wrong" << endl;
20
+ return false ;
21
+ }
22
+
23
+ double ResultMinute = minutes + (hours*60 ) + (seconds/60 );
24
+ cout << " The result minute is " << ResultMinute<<endl;
25
+
26
+ return true ;
11
27
}
12
28
13
- int main3 ()
29
+ int main ()
14
30
{
15
- double hours, minutes, seconds;
16
- cout << " Hours: " ;
17
- cin >> hours; // Prompting user for data and
18
- cout << " Minutes: " ; // reading 3 numbers for user
19
- cin >> minutes; // !
20
- cout << " Seconds: " ;
21
- cin >> seconds;
22
- if (hours >= 0 && minutes >= 0 && seconds >= 0 ) // the program will be executed when all numbers are positive
23
- cout << " The time in minutes is " << converting1 (hours, minutes, seconds) << endl;
24
- else // calling function to calculate the main function
25
- cout << " Invalid inputs!" << endl;
31
+ double InputHours, InputMinutes, InputSeconds;
32
+ bool bIsGetSuccess = 0 ;
33
+
34
+ do {
35
+
36
+ if (!(cin >> InputHours >> InputMinutes >> InputSeconds)) {
37
+ // Input error occurred
38
+ cin.clear (); // Clear the error state
39
+ cin.ignore (std::numeric_limits<std::streamsize>::max (), ' \n ' ); // Clear the input buffer
40
+ cout << " Please enter again , you enter the wrong value" << endl;
41
+ continue ;
42
+ }
43
+
44
+ bIsGetSuccess = GetMinutesFromTime (InputHours, InputMinutes, InputSeconds);
45
+
46
+
47
+ } while (!bIsGetSuccess);
48
+
26
49
return 0 ; // indicates that the program will ended successfully
27
- }
50
+ }
0 commit comments