forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.Ex1 QueryVector.cpp
57 lines (49 loc) · 1.31 KB
/
17.Ex1 QueryVector.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
#include <vector>
#include <iostream>
using namespace std;
char DisplayOptions()
{
cout << "What would you like to do?" << endl;
cout << "Select 1: To enter an integer" << endl;
cout << "Select 2: Query a value given an index" << endl;
cout << "Select 3: To display the vector" << endl;
cout << "Select 4: To quit!" << endl << "> ";
char ch;
cin >> ch;
return ch;
}
int main()
{
vector <int> vecData;
char chUserChoice = '\0';
while((chUserChoice = DisplayOptions()) != '4')
{
if(chUserChoice == '1')
{
cout << "Please enter an integer to be inserted: ";
int nDataInput = 0;
cin >> nDataInput;
vecData.push_back(nDataInput);
}
else if(chUserChoice == '2')
{
cout << "Please enter an index between 0 and ";
cout << (vecData.size() - 1) << ": ";
size_t index = 0;
cin >> index;
if(index < (vecData.size()))
{
cout << "Element [" << index << "] = " << vecData[index];
cout << endl;
}
}
else if(chUserChoice == '3')
{
cout << "The contents of the vector are: ";
for(size_t index = 0; index < vecData.size(); ++index)
cout << vecData[index] << ' ';
cout << endl;
}
}
return 0;
}