-
Notifications
You must be signed in to change notification settings - Fork 0
/
MenuInputValidation.cpp
76 lines (70 loc) · 2.58 KB
/
MenuInputValidation.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**********************************************************
** Program name: MenuInputValidation.cpp
** Author: Ben Fridkis
** Date: 5/3/2017
** Description: MenuInputValidation class member function
definitions.
***********************************************************/
#include "MenuInputValidation.hpp"
/*##################################################
####################################################
Derivative Class Member Functions:
MenuInputValidation
####################################################
####################################################*/
/****************************************************
MenuInputValidation
Function to check input stream for a valid menu
selection entry. Assumes menu entry options begin
at 1. Function is passed an int parameter to
establish the greatest entry value. Assumes all
entries between 1 and numberOfSelections parameter
are integers and valid. Base class is Menu.
****************************************************/
int MenuInputValidation::menuInputValidation
(int numberOfSelections)
{
int userChoice;
cin >> userChoice;
//If user input generates an error because
//a non-int type is received by the input
//stream, or a value is entered that is out
//of the established range of selections, loop
//as follows:
while (!cin || userChoice < 1 || userChoice > numberOfSelections)
{
cout << "\nPlease enter an integer between 1 and "
<< numberOfSelections<< " to make your selection.\n"
<< endl;
//Clear the cin error flag.
cin.clear();
//Ignore the rest of the input stream, in
//case space seperated values were provided.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//Try again.
cin >> userChoice;
}
//Ignore any remaining values in the input
//stream, and print a new line.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl;
userSelection = userChoice;
return userSelection;
}
/****************************************
getUserSelection
Returns userSelection member variable.
****************************************/
int MenuInputValidation::getUserSelection() const
{
return userSelection;
}
/***************************************
setUserSelection
Sets userSelection member variable.
***************************************/
void MenuInputValidation::setUserSelection
(int userSelection)
{
this->userSelection = userSelection;
}