-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex38.cpp
More file actions
81 lines (70 loc) · 1.26 KB
/
Copy pathex38.cpp
File metadata and controls
81 lines (70 loc) · 1.26 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
CPSC 121-0X
Paul De Palma
depalma
Example 38
*/
//Demonstrates error-checking with two inputs
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
/*
Pre: None
Post: returns an integer int the range [0,25]
*/
int getInt();
/*
Pre: None
Post: returns an character in the range ['A','F']
*/
char getChar();
int main()
{
int num = getInt();
char ch = getChar();
cout << num << " " << ch << endl;
return 0;
}
char getChar()
{
char ch;
while(true)
{
cout << "Enter an upper case character in the ";
cout << "range ['A','F']" << endl << endl;
if (cin >> ch)
if (ch >= 'A' && ch <= 'F')
break;
cin.clear();
cin.ignore();
cout << "Invalid input. Input must be character in the ";
cout << "range ['A','F']" << endl << endl;
}
cin.clear();
cin.ignore();
return ch;
}
int getInt()
{
float numIn;
int num;
while(true)
{
cout << "Enter an integer in the range [0,25]" << endl;
if (cin >> numIn)
if (floor(numIn) == numIn)
{
num = static_cast<int>(numIn);
if (num >= 0 && num <= 25)
break;
}
cin.clear();
cin.ignore();
cout << "Invalid input. Input must be an integer in the ";
cout << "range [0,25]" << endl << endl;
}
cin.clear();
cin.ignore();
return num;
}