forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_validation_do_while.cpp
More file actions
62 lines (53 loc) · 2.34 KB
/
input_validation_do_while.cpp
File metadata and controls
62 lines (53 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************************************************************************
*
* Program: User Input Validation With A Do-While Loop
*
* Description: Demonstration of how to perform user input validation with a
* do-while loop in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=zekDB52A4Cw
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
int main()
{
// Declare an int variable month to store the month the user will enter.
int month;
// We need to prompt the user to enter in the month at least once, but perhaps
// multiple times if the user does not enter a valid month from 1-12. This is
// a good situation to use a do-while loop, because that's exactly how a
// do-while loop works... the body will execute at least once, but perhaps
// multiple times if the condition of the loop evaluates to true (i.e. if
// it's necessary to execute the loop body again).
do
{
// Prompt the user to enter in a month from 1-12. We can't use a month like
// "13" in our program that is outside of this range because it may cause
// a bug when working with months as it is an invalid month.
cout << "Month (1-12): ";
// Store the int that the user enters into the month variable
cin >> month;
// If the month is outside of the valid range, we output an input validation
// error message to the user explaining what they've done wrong so that they
// can avoid this mistake next time. If the month is less than or equal to
// 0 OR it's greater than or equal to 13, we know it's not in the valid
// range of months from 1-12.
if (month <= 0 || month >= 13)
{
cout << "Invalid month entered!" << endl;
cout << "Month must be between 1-12." << endl;
}
// Continue the loop until a valid month is entered, when the loop body
// executes on a 2nd iteration and onward, the user will again be asked
// to enter in a valid month, and again they'll be provided with an input
// validation error message if they enter in an invalid month.
}
while (month <= 0 || month >= 13);
// The loop will stop when a valid month has been entered, and we output
// that month here!
cout << "Month Entered: " << month << endl;
return 0;
}