-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlab_adaptive.cpp
More file actions
77 lines (60 loc) · 1.59 KB
/
Copy pathlab_adaptive.cpp
File metadata and controls
77 lines (60 loc) · 1.59 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
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
using namespace std;
/* Getting input from the user in C++ is simply a matter
of saying:
int input;
cin >> input;
However, getting C++ to handle invalid input is
surprisingly complex. So here is a input() function
that I have written that does it all for you.
It behaves in a similar way to input() in Python
and will raise a runtime_error exception if the user
trys to enter anything that can't be converted to the
correct type. You don't need to know how this work at
the moment, just type...
input<int>();
... to read in ints or ...
input<float>();
..if you watch to read floats etc. . */
template<typename T>
T input()
{
string buffer;
getline(cin, buffer);
stringstream ss(buffer);
T input;
if( ss >> input && ss.eof() )
return input;
throw runtime_error("Input is of invalid type");
}
int main()
{
int size = 0;
// get the size
while( size <= 0 )
{
cout << "Enter a size: ";
try
{
size = input<int>();
if( size <= 0 )
cout << "Not a valid size" << endl;
}
catch( runtime_error &e )
{
cerr << "Not an integer" << endl;
}
}
cout << "Reading in " << size << " values." << endl;
int *values;
// COMPLETE ME
// print everything in reverse
for( int i=size-1; i>=0; --i )
{
cout << "Element " << i << " is " << values[i] << endl;
}
return 0;
}