forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_init_user_input.cpp
More file actions
95 lines (78 loc) · 3.12 KB
/
vector_init_user_input.cpp
File metadata and controls
95 lines (78 loc) · 3.12 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
*
* Program: Initialize A Vector With User Input
*
* Description: Program to initialize a vector with user input in C++ using two
* different approaches. In the first approach we know the number of elements to
* initialize in advance of prompting the user for the values. In the second
* approach there is an indeterminate number of elements to be initialized and a
* sentinel value is used to terminate user input.
*
* YouTube Lesson: https://www.youtube.com/watch?v=18r4P6UNSYk
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// In the first approach, which is commented out, we first prompt the user
// for the number of elements. We then initialize a vector with this size,
// and prompt the user for each element value and set the element value
// using the element index. To test this code you can uncomment the below
// code and comment out the code for the second approach.
//
/*
// Prompt the user for the number of elements to be entered, store the result
// into total
cout << "Number of Elements: ";
int total = 0;
cin >> total;
// Create a vector of size total to store ints
vector<int> data(total);
// Prompt the user for total number of int values, store them into the vector
for (int i = 0; i < total; i++)
{
// Output i so the user knows the index of the element being set
cout << "Element " << i << ": ";
// Store the value in the vector at index i
cin >> data[i];
}
*/
// In the second approach, we don't know how many values the user will enter
// before we begin asking the user to provide the values. We use a sentinel
// value (-1) to terminate user input in this case. We create a vector with
// no initialize size provided and add vector elements using the push_back()
// method until the sentinel value is entered.
// Create the vector
vector<int> data;
// i will be used to output the element index being set, value will store
// each value entered by the user
int i = 0;
int value = 0;
// inform the user how to terminate user input with the -1 sentinel value
cout << "Enter -1 To Complete Input!" << endl;
// the loop will run an indeterminate number of times
while (true)
{
// prompt the user for the value and store it into the value variable
cout << "Element " << i << ": ";
cin >> value;
// if the user enters -1, terminate the loop to complete user input
if (value == -1) break;
// if the sentinel value was not entered, add the value as the next
// element in the vector using push_back()
data.push_back(value);
// increment i so that the correct index is output when the user is
// prompted for the next element value
i++;
}
// Print out the vector elements using a range-based for loop to verify that
// the vector element values were set correctly
for (auto elem : data)
cout << elem << " ";
cout << endl;
return 0;
}