Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d9d00f

Browse files
committed
Lab 5 Convert Time
- Convert time to minutes - Learn cin fail and clear
1 parent 8b8ad77 commit 2d9d00f

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

‎OOP1-Lab5/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@
1616

1717
3. Write a program to convert time to minutes using functions. (input 3 variables namely hours, minutes and seconds. Convert everything into minutes.)
1818

19+
**Cin.fail Cin.clear Cin.ignore**
20+
21+
We can use this cin function to judge the user input's correctless. If cin.fail is 1, maybe our input type is wrong with the store variable type.
22+
23+
cin.clear(); // Clear the error state
24+
25+
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');//clear all input in the string buffer
26+
27+
28+
1929
4. Write a program to sum the series up to n (Input n)

‎OOP1-Lab5/Source2.cpp‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,46 @@
55
#include <iostream>
66
using namespace std;
77

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;
1127
}
1228

13-
int main3()
29+
int main()
1430
{
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+
2649
return 0; // indicates that the program will ended successfully
27-
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /